package com.storlead.system.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.storlead.framework.common.result.Result;
import com.storlead.system.dto.CompanyDTO;
import com.storlead.system.util.SystemSettingCacheCheckUtil;
import com.storlead.user.pojo.entity.CompanyEntity;
import com.storlead.user.pojo.entity.SubCompanyEntity;
import com.storlead.user.service.ICompanyService;
import com.storlead.user.service.ISubCompanyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import javax.annotation.Resource;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.Objects;

/**
 * @program: sp-sales
 * @description:
 * @author: chenkq
 * @create: 2022-06-29 17:33
 */

@Api(tags = "平台中心 - 系统: 公司数据维护")
@RestController
@RequestMapping("/sys/company")
public class CompanyApiController {

    @Resource
    private ICompanyService companyService;

    @Resource
    private ISubCompanyService subCompanyService;

    @ApiOperation("新增公司")
    @PostMapping("save")
    public Result save(CompanyEntity company) {
        Result r = SystemSettingCacheCheckUtil.checkAddDataMode();
        if (!r.isSuccess()) {
            return r;
        }
        if(Objects.isNull(company.getId())) {
            company.setDataSource(1);
            LambdaQueryWrapper<CompanyEntity> lm = new LambdaQueryWrapper();
            lm.eq(CompanyEntity::getDataSource,1);
            lm.orderByDesc(CompanyEntity::getRouteCode);
            lm.last("limit 1");
            CompanyEntity top = companyService.getOne(lm);
            if (Objects.nonNull(top)) {
                Integer code = Integer.valueOf(top.getRouteCode());
                company.setRouteCode(String.valueOf(code+1));
            }else {
                company.setRouteCode("150");
            }
        }
        companyService.saveOrUpdate(company);
        return Result.ok();
    }

    @ApiOperation("分页查询公司")
    @PostMapping("pagelist")
    public Result save(CompanyDTO param) {
        IPage<CompanyEntity> page = new Page<>(param.getPageIndex(),param.getPageSize());
        IPage<CompanyEntity> companys = companyService.page(page);
        return Result.result(companys);
    }

    @ApiOperation("删除总公司信息")
    @PostMapping("delete")
    public Result delete(Long [] ids) {
        if (ids == null || ids.length == 0) {
            return Result.error("参数错误");
        }
        Integer exist = subCompanyService.count(new LambdaQueryWrapper<SubCompanyEntity>().in(SubCompanyEntity::getCompanyId, Arrays.asList(ids)));
        if (!Objects.equals(exist,Integer.valueOf(0))) {
            return Result.error("公司下有关联子公司,无法删除!");
        }
        companyService.removeByIds(Arrays.asList(ids));
        return Result.ok();
    }

}
