package com.zhgd.xmgl.modules.basicdata.controller.enterprise;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.Company;
import com.zhgd.xmgl.modules.basicdata.service.ICompanyService;
import com.zhgd.xmgl.modules.basicdata.service.IEngineeringService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


/**
 * @Title: Controller
 * @Description: 参建单位
 * @author： pengj
 * @date： 2023-03-13
 * @version： V1.0
 */
@RestController
@RequestMapping("/ent/company")
@Slf4j
@Api(tags = "参建单位管理")
public class EntCompanyController {
    @Autowired
    private ICompanyService companyService;

    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     *
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "分页查询", operDesc = "分页列表查询参建单位信息")
    @ApiOperation(value = " 分页列表查询参建单位信息", notes = "分页列表查询参建单位信息", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "type", value = "参建单位类型", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "companyName", value = "单位名称   ", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "engineeringSn", value = "工程sn   ", paramType = "query", dataType = "String")
   })
    @PostMapping(value = "/page")
    public Result<IPage<Company>> queryPageList(Page page, @RequestBody Company company) {
        LambdaQueryWrapper<Company> wrapper = Wrappers.<Company>lambdaQuery();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(company.getType())) {
            wrapper.like(Company::getType, company.getType());
        }
        if (StringUtils.isNotBlank(company.getCompanyName())) {
            wrapper.like(Company::getCompanyName, company.getCompanyName());
        }
        if (StringUtils.isNotBlank(company.getEngineeringSn())) {
            wrapper.eq(Company::getEngineeringSn, company.getEngineeringSn());
        }/* else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in(Company::getEngineeringSn, engineeringSns);
            }
        }*/
        IPage<Company> pageList = companyService.page(page, wrapper);
        return Result.success(pageList);
    }

    /**
     * 列表查询
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "列表查询", operDesc = "列表查询参建单位信息")
    @ApiOperation(value = " 列表查询参建单位信息", notes = "列表查询参建单位信息", httpMethod = "POST")
    @ApiImplicitParam(name = "engineeringSn", value = "工程sn   ", paramType = "query", dataType = "String")
    @PostMapping(value = "/list")
    public Result<List<Company>> queryList(@RequestBody Company company) {
        LambdaQueryWrapper<Company> wrapper = Wrappers.<Company>lambdaQuery();
//        SecurityUser user = SecurityUtil.getUser();
        if (StringUtils.isNotBlank(company.getEngineeringSn())) {
            wrapper.eq(Company::getEngineeringSn, company.getEngineeringSn());
        } /*else {
            List<String> engineeringSns = engineeringService.getSnListByEnterprise(user.getSn());
            if (engineeringSns.size() > 0) {
                wrapper.in(Company::getEngineeringSn, engineeringSns);
            }
        }*/
        List<Company> list = companyService.list(wrapper);
        return Result.success(list);
    }


    /**
     * 添加
     *
     * @param company
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "新增", operDesc = "添加参建单位信息")
    @ApiOperation(value = " 添加参建单位信息", notes = "添加参建单位信息", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Object> add(@RequestBody Company company) {
        companyService.saveInfo(company);
        return Result.success("添加成功！");
    }

    /**
     * 编辑
     *
     * @param company
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "修改", operDesc = "编辑参建单位信息")
    @ApiOperation(value = "编辑参建单位信息", notes = "编辑参建单位信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<Company> edit(@RequestBody Company company) {
        Result<Company> result = new Result<Company>();
        Company companyEntity = companyService.getById(company.getId());
        if (companyEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = companyService.updateById(company);
            if (ok) {
                result.success("修改成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }

    /**
     * 通过id删除
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "删除", operDesc = "删除参建单位信息")
    @ApiOperation(value = "删除参建单位信息", notes = "删除参建单位信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "参建单位ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/delete")
    public Result<Company> delete(@RequestBody Company company) {
        Result<Company> result = new Result<Company>();
        Company companyEntity = companyService.getById(company.getId());
        if (companyEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = companyService.removeById(company.getId());
            if (ok) {
                result.success("删除成功!");
            }
            result.success("操作失败!");
        }

        return result;
    }


    /**
     * 通过id查询
     * @return
     */
    @OperLog(operModul = "参建单位管理", operType = "查询", operDesc = "通过id查询参建单位信息")
    @ApiOperation(value = "通过id查询参建单位信息", notes = "通过id查询参建单位信息", httpMethod = "POST")
    @ApiImplicitParam(name = "id", value = "参建单位ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<Company> queryById(@RequestBody Company company) {
        Result<Company> result = new Result<Company>();
        Company companyEntity = companyService.getById(company.getId());
        if (companyEntity == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(companyEntity);
            result.setSuccess(true);
        }
        return result;
    }
}
