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

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhgd.annotation.OperLog;
import com.zhgd.jeecg.common.api.vo.Result;
import com.zhgd.xmgl.modules.basicdata.entity.Government;
import com.zhgd.xmgl.modules.basicdata.entity.Project;
import com.zhgd.xmgl.modules.basicdata.entity.SystemUser;
import com.zhgd.xmgl.modules.basicdata.service.IGovernmentService;
import com.zhgd.xmgl.modules.basicdata.service.IProjectService;
import com.zhgd.xmgl.modules.basicdata.service.ISystemUserService;
import com.zhgd.xmgl.modules.basicdata.vo.ProjectVo;
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.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 java.util.Arrays;
import java.util.Map;


/**
 * @Title: Controller
 * @Description: 项目信息
 * @author： pds
 * @date： 2023-02-15
 * @version： V1.0
 */
@RestController
@RequestMapping("/project/project")
@Slf4j
@Api(tags = "项目信息")
public class ProjectController {
    @Autowired
    private IProjectService projectService;

    @Autowired
    private ISystemUserService systemUserService;

    @Autowired
    private IGovernmentService governmentService;

    /**
     * 添加
     *
     * @param projectVo
     * @return
     */
    @OperLog(operModul = "项目管理", operType = "新增", operDesc = "项目注册")
    @ApiOperation(value = " 项目注册", notes = "项目注册", httpMethod = "POST")
    @PostMapping(value = "/add")
    public Result<Project> add(@RequestBody ProjectVo projectVo) {
        Result<Project> result = new Result<Project>();
        // 检验该政务项目数是否已满
        int install = projectService.count(Wrappers.<Project>lambdaQuery().eq(Project::getGovernmentSn, projectVo.getGovernmentSn()));
        int auth = governmentService.getOne(Wrappers.<Government>lambdaQuery().eq(Government::getGovernmentSn, projectVo.getGovernmentSn())).getAuthProject();
        if (install >= auth) {
            result.error500("该住建局授权项目数已达上限!");
            return result;
        }
        // 检验项目账号是否已存在
        SystemUser systemUser = systemUserService.getOne(Wrappers.<SystemUser>lambdaQuery().eq(SystemUser::getAccount, projectVo.getAccount()));
        if (systemUser != null) {
            result.error500("该用户账号已被使用");
            return result;
        }
        projectService.saveInfo(projectVo);
        result.success("添加成功！");
        return result;
    }

    /**
     * 编辑
     *
     * @param project
     * @return
     */
    @OperLog(operModul = "项目管理", operType = "修改", operDesc = "编辑项目信息")
    @ApiOperation(value = "编辑项目信息", notes = "编辑项目信息", httpMethod = "POST")
    @PostMapping(value = "/edit")
    public Result<Project> edit(@RequestBody Project project) {
        Result<Project> result = new Result<Project>();
        Project projectEntity = projectService.getById(project.getProjectId());
        if (projectEntity == null) {
            result.error500("未找到对应实体");
        } else {
            boolean ok = projectService.updateById(project);
            if (ok) {
                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<Project> delete(@RequestBody Project projectEntity) {
        Result<Project> result = new Result<Project>();
        boolean ok = projectService.removeById(projectEntity.getProjectId());
        if (ok) {
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 批量删除
     *
     * @return
     */
    @OperLog(operModul = "项目管理", operType = "批量删除", operDesc = "批量删除项目信息")
    @ApiOperation(value = "批量删除项目信息", notes = "批量删除项目信息", httpMethod = "POST")
    @ApiImplicitParam(name = "ids", value = "项目信息ID字符串", paramType = "query", required = true, dataType = "String")
    @PostMapping(value = "/deleteBatch")
    public Result<Project> deleteBatch(@RequestBody Map<String, Object> map) {
        String ids = MapUtils.getString(map, "ids");
        Result<Project> result = new Result<Project>();
        if (ids == null || "".equals(ids.trim())) {
            result.error500("参数不识别！");
        } else {
            this.projectService.removeByIds(Arrays.asList(ids.split(",")));
            result.success("删除成功!");
        }
        return result;
    }

    /**
     * 通过id查询
     *
     * @return
     */
    @OperLog(operModul = "项目管理", operType = "查询", operDesc = "通过id查询项目信息信息")
    @ApiOperation(value = "通过id查询项目信息信息", notes = "通过id查询项目信息信息", httpMethod = "POST")
    @ApiImplicitParam(name = "projectId", value = "项目信息ID", paramType = "query", required = true, dataType = "Integer")
    @PostMapping(value = "/queryById")
    public Result<Project> queryById(@RequestBody Project projectEntity) {
        Result<Project> result = new Result<Project>();
        Project project = projectService.getById(projectEntity.getProjectId());
        if (project == null) {
            result.error500("未找到对应实体");
        } else {
            result.setResult(project);
            result.setSuccess(true);
        }
        return result;
    }

    /**
     * 查询当前登录账号的项目信息
     *
     * @param
     * @return
     */
    @OperLog(operModul = "项目管理", operType = "列表查询", operDesc = "查询当前账号对应的项目信息")
    @ApiOperation(value = " 查询当前账号对应的项目信息", notes = "查询当前账号对应的项目信息", httpMethod = "POST")
    @PostMapping(value = "/queryBySelf")
    public Result<Project> queryBySelf() {
        Result<Project> result = new Result<Project>();
        SecurityUser user = SecurityUtil.getUser();
        if (user.getAccountType() != 4) {
            result.error500("未找到对应信息");
        } else {
            Project project = projectService.getOne(Wrappers.<Project>lambdaQuery().eq(Project::getProjectSn, user.getSn()));
            result.setSuccess(true);
            result.setResult(project);
        }
        return result;
    }
}
