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.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.jeecg.common.system.query.QueryGenerator;
import com.zhgd.jeecg.common.util.PageUtil;
import com.zhgd.xmgl.modules.basicdata.entity.Engineering;
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.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-22
 * @version： V1.0
 */
@RestController
@RequestMapping("/gov/engineering")
@Slf4j
@Api(tags = "工程信息")
public class GovEngineeringController {
    @Autowired
    private IEngineeringService engineeringService;

    /**
     * 分页列表查询
     * @return
     */
    @OperLog(operModul = "工程管理", operType = "分页查询", operDesc = "分页列表查询项目报监审批")
    @ApiOperation(value = " 分页列表查询项目报监审批", notes = "分页列表查询项目报监审批", httpMethod = "POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "engineeringCode", value = "工程编码", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "engineeringName", value = "工程名称", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "engineeringType", value = "工程类别", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "state", value = "工程状态(1:未开工;)", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "examineState", value = "1:待审批;2:审批驳回;3:审批通过;", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "startTime", value = "开始时间", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "endTime", 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<Engineering>> queryPageList(@RequestBody Map<String, Object> map) {
        Result<IPage<Engineering>> result = new Result<IPage<Engineering>>();
        QueryWrapper<Engineering> wrapper = QueryGenerator.initPageQueryWrapper(Engineering.class, map);
        Page<Engineering> page = PageUtil.getPage(map);
        IPage<Engineering> pageList = engineeringService.getPageList(page, wrapper);
        result.setSuccess(true);
        result.setResult(pageList);
        return result;
    }

    /**
     * 列表查询
     *
     * @param engineering
     * @param req
     * @return
     */
    @OperLog(operModul = "工程管理", operType = "列表查询", operDesc = "列表查询项目工程表信息")
    @ApiOperation(value = " 列表查询项目工程表信息", notes = "列表查询项目工程表信息", httpMethod = "POST")
    @PostMapping(value = "/list")
    public Result<List<Engineering>> queryList(@RequestBody Engineering engineering, HttpServletRequest req) {
        QueryWrapper<Engineering> queryWrapper = QueryGenerator.initQueryWrapper(engineering, req.getParameterMap());
        List<Engineering> list = engineeringService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     *  编辑
     * @param engineering
     * @return
     */
    @OperLog(operModul = "工程管理", operType = "编辑", operDesc = "编辑项目工程表信息")
    @ApiOperation(value = "编辑项目工程表信息", notes = "编辑项目工程表信息" , httpMethod="POST")
    @PostMapping(value = "/edit")
    public Result<Engineering> edit(@RequestBody Engineering engineering) {
        Result<Engineering> result = new Result<Engineering>();
        Engineering engineeringEntity = engineeringService.getById(engineering.getId());
        if(engineeringEntity==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = engineeringService.updateById(engineering);
            if(ok) {
                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<Engineering> queryById(@RequestBody Engineering engineeringEntity) {
        Result<Engineering> result = new Result<Engineering>();
        Engineering engineering = engineeringService.getById(engineeringEntity.getId());
        if (engineering == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(engineering);
            result.setSuccess(true);
        }
        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 = "/queryDetailById")
    public Result<Engineering> queryDetailById(@RequestBody Engineering engineeringEntity) {
        Result<Engineering> result = new Result<Engineering>();
        Engineering engineering = engineeringService.getById(engineeringEntity.getId());
        if (engineering == null) {
            result.error500("未找到对应实体");
        } else {
            engineering = engineeringService.queryDetailById(engineering);
            result.setResult(engineering);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 查询项目下的工程信息
     *
     * @return
     */
    @OperLog(operModul = "工程管理", operType = "查询", operDesc = "查询项目下的工程信息")
    @ApiOperation(value = "查询项目下的工程信息", notes = "查询项目下的工程信息", httpMethod = "POST")
    @ApiImplicitParam(name = "projectSn", value = "项目sn", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/queryByProject")
    public Result<List<Engineering>> queryByProject(@RequestBody Map<String, Object> map) {
        String projectSn = MapUtils.getString(map, "projectSn");
        List<Engineering> engineeringList = engineeringService.list(Wrappers.<Engineering>lambdaQuery().eq(Engineering::getProjectSn, projectSn));
        return Result.success(engineeringList);
    }

    @OperLog(operModul = "工程管理", operType = "修改", operDesc = "报监审批")
    @ApiOperation(value = "报监审批", notes = "报监审批" , httpMethod="POST")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "工程ID", paramType = "query", required = true, dataType = "String"),
            @ApiImplicitParam(name = "suggest", value = "审核结果(2:驳回;3:同意)", paramType = "query", required = true, dataType = "Integer"),
            @ApiImplicitParam(name = "rejectReason", value = "驳回原因", paramType = "query", required = true, dataType = "Integer")
    })
    @PostMapping(value = "/examine")
    public Result<Engineering> examine(@RequestBody Map<String,Object> map) {
        Result<Engineering> result = new Result<Engineering>();
        Engineering engineering = engineeringService.getById(MapUtils.getString(map, "id"));
        if(engineering==null) {
            result.error500("未找到对应实体");
        }else {
            boolean ok = engineeringService.examine(map);
            if(ok) {
                result.success("修改成功!");
            }
        }
        return result;
    }
}
