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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.jeecg.common.system.query.QueryGenerator;
import com.zhgd.xmgl.modules.basicdata.entity.Enterprise;
import com.zhgd.xmgl.modules.basicdata.service.IEnterpriseService;
import com.zhgd.xmgl.modules.basicdata.vo.EnterpriseVo;
import com.zhgd.xmgl.security.SecurityUser;
import com.zhgd.xmgl.security.SecurityUtil;
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.apache.commons.collections.MapUtils;
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 javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 企业信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/gov/enterprise")
@Slf4j
@Api(tags = "企业信息")
public class GovEnterpriseController {
    @Autowired
    private IEnterpriseService enterpriseService;

    /**
     * 政务分页列表查询企业
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "分页查询", operDesc = "政务分页列表查询企业")
    @ApiOperation(value = " 政务分页列表查询企业", notes = "政务分页列表查询企业", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "creditCode", value = "社会信用代码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "enterpriseName", value = "企业名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "pageNo", value = "页数", paramType = "query", required = true, defaultValue = "1", dataType = "Integer"),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", paramType = "query", required = true, defaultValue = "10", dataType = "Integer")
    })
    @PostMapping(value = "/page")
    public Result<IPage<Enterprise>> queryPageList(@RequestBody Map<String, Object> map) {
        return Result.success(enterpriseService.pageList(map));
    }

    /**
     * 政务列表查询企业
     *
     * @param enterprise
     * @param req
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "列表查询", operDesc = "政务列表查询企业")
    @ApiOperation(value = " 政务列表查询企业", notes = "政务列表查询企业", httpMethod = "POST")
    @PostMapping(value = "/list")
    public Result<List<Enterprise>> queryPageList(@RequestBody Enterprise enterprise, HttpServletRequest req) {
        SecurityUser user = SecurityUtil.getUser();
        enterprise.setGovernmentSn(user.getSn());
        QueryWrapper<Enterprise> queryWrapper = QueryGenerator.initQueryWrapper(enterprise, req.getParameterMap());
        List<Enterprise> list = enterpriseService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 企业审批
     *
     * @param map
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "修改", operDesc = "企业审批")
    @ApiOperation(value = "企业审批", notes = "企业审批", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "enterpriseId", value = "企业信息ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "suggest", value = "审核结果(0:驳回;1:同意)", paramType = "query", required = true, dataType = "Integer"),
    })
    @PostMapping(value = "/examine")
    public Result<Enterprise> examine(@RequestBody Map<String, Object> map) {
        Result<Enterprise> result = new Result<Enterprise>();
        String enterpriseId = MapUtils.getString(map, "enterpriseId");
        Enterprise enterpriseEntity = enterpriseService.getById(enterpriseId);
        if (enterpriseEntity == null) {
            result.error500("未找到对应实体");
        } else {
            Integer suggest = MapUtils.getInteger(map, "suggest");
            boolean ok = enterpriseService.updateInfo(enterpriseEntity, suggest);
            if (ok) {
                result.success("修改成功!");
            }
        }

        return result;
    }


    /**
     * 通过id查询
     *
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "查询", operDesc = "通过id查询企业信息")
    @ApiOperation(value = "通过id查询企业信息", notes = "通过id查询企业信息", httpMethod = "POST")
    @ApiImplicitParam(name = "enterpriseId", value = "企业信息ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryById")
    public Result<Enterprise> queryById(@RequestBody Enterprise enterpriseEntity) {
        Result<Enterprise> result = new Result<Enterprise>();
        Enterprise enterprise = enterpriseService.getById(enterpriseEntity.getEnterpriseId());
        if (enterprise == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(enterprise);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @OperLog(operModul = "企业管理", operType = "查询", operDesc = "通过id查询企业详细信息")
    @ApiOperation(value = "通过id查询企业详细信息", notes = "通过id查询企业详细信息", httpMethod = "POST")
    @ApiImplicitParam(name = "enterpriseId", value = "企业信息ID", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/getDetailById")
    public Result<EnterpriseVo> getDetailById(@RequestBody Enterprise enterpriseEntity) {
        Result<EnterpriseVo> result = new Result<EnterpriseVo>();
        Enterprise enterprise = enterpriseService.getById(enterpriseEntity.getEnterpriseId());
        if (enterprise == null) {
            result.error500("未找到对应实体");
        } else {
            EnterpriseVo enterpriseVo = enterpriseService.getDetailById(enterpriseEntity.getEnterpriseId());
            result.setResult(enterpriseVo);
            result.setSuccess(true);
        }
        return result;
    }
}
