package com.yccrc.kede.controller.ent;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yccrc.common.annotation.Log;
import com.yccrc.common.core.controller.BaseController;
import com.yccrc.common.core.redis.RedisCache;
import com.yccrc.common.enums.BusinessType;
import com.yccrc.common.exception.ServiceException;
import com.yccrc.common.vo.Result;
import com.yccrc.kede.domain.Project;
import com.yccrc.kede.service.INotifyService;
import com.yccrc.kede.service.IProjectLogService;
import com.yccrc.kede.service.IProjectService;
import com.yccrc.kede.vo.ProjectAddVO;
import com.yccrc.kede.vo.ProjectDetailVO;
import com.yccrc.kede.vo.ProjectListVO;
import com.yccrc.kede.vo.ProjectQueryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;


/**
 * 项目信息Controller
 *
 * @author yccrc
 * @date 2022-04-25
 */
@Api(tags = "项目信息")
@RestController
@RequestMapping("/ent/project")
public class ProjectController extends BaseController {
    @Autowired
    IProjectLogService projectLogService;
    @Autowired
    private IProjectService projectService;
    @Autowired
    private INotifyService notifyService;
    @Autowired
    private RedisCache redisCache;

    @ApiOperation(value = "项目管理列表")
    @GetMapping("/manageList")
    public Result<Page<ProjectListVO>> manageList(ProjectQueryVO project) {
        return new Result<Page<ProjectListVO>>().ok(projectService.selectProject(project));
    }

    @ApiOperation(value = "项目申报列表")
    @GetMapping("/reportList")
    public Result<Page<Project>> reportList(ProjectQueryVO project) {
        return new Result<Page<Project>>().ok(projectService.selectReportProject(project));

    }

    @ApiOperation(value = "获取项目信息详细信息")
    @GetMapping(value = "/{id}")
    public Result<ProjectDetailVO> getInfo(@PathVariable("id") Integer id) {
        return new Result<ProjectDetailVO>().ok(projectService.getProject(id));
    }

    @ApiOperation(value = "新增项目信息")
    @Log(title = "项目信息", businessType = BusinessType.INSERT)
    @PostMapping
    public Result<Boolean> add(@RequestBody ProjectAddVO project) {
        return new Result<Boolean>().ok(projectService.saveProject(project));
    }

    @ApiOperation(value = "修改项目信息")
    @Log(title = "项目信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public Result<Boolean> edit(@RequestBody Project project) {
        projectLogService.saveLog(project.getId(), "项目信息修改");
        // 三个项目名称中修改一个项目名称,另外两个同步修改
        Project one = projectService.getOne(new LambdaQueryWrapper<Project>()
                .select(Project::getId, Project::getProjectRefId)
                .eq(Project::getId, project.getId()));
        // 修改主项目,同步两个从项目 修改从项目,同步一个主项目一个从项目
        projectService.update(new LambdaUpdateWrapper<Project>()
                .set(Project::getProjectName, project.getProjectName())
                .or().eq(one.getProjectRefId() == null, Project::getProjectRefId, project.getId())
                .or().eq(one.getProjectRefId() != null, Project::getProjectRefId, one.getProjectRefId())
                .or().eq(one.getProjectRefId() != null, Project::getId, one.getProjectRefId())
                .or().eq(Project::getId, project.getId()));
        redisCache.deleteObject("project-draft:" + project.getId());
        return new Result<Boolean>().ok(projectService.updateById(project));
    }

    @ApiOperation(value = "保存草稿")
    @Log(title = "保存草稿", businessType = BusinessType.OTHER)
    @PostMapping("/save-draft")
    public Result<Boolean> saveDraft(@RequestBody Project project) {
        redisCache.setCacheObject("project-draft:" + project.getId(), project);
        return new Result<Boolean>().ok(true);
    }

    @ApiOperation(value = "获取草稿")
    @Log(title = "获取草稿", businessType = BusinessType.OTHER)
    @GetMapping("/draft/{projectId}")
    public Result<Project> draft(@PathVariable Integer projectId) {
        Project project = redisCache.getCacheObject("project-draft:" + projectId);
        if (project == null) {
            return new Result<Project>().error("暂无草稿信息");
        }
        return new Result<Project>().ok(project);
    }

    @ApiOperation(value = "删除项目信息")
    @Log(title = "项目信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public Result<Boolean> remove(@PathVariable Integer[] ids) {
        //return new Result<Boolean>().ok(projectService.removeBatchByIds(Arrays.asList(ids)));
        return new Result<Boolean>().ok(projectService.remove(ids));
    }

    @ApiOperation(value = "同步给财务")
    @PutMapping("/sync/finc/{id}")
    public Result<Boolean> syncFinc(@PathVariable Integer id) {
        projectLogService.saveLog(id, "同步给财务");
        return projectService.syncProject(id, 3)
                ? new Result<Boolean>().ok(true) : new Result<Boolean>().error("无可同步的信息");
    }

    @ApiOperation(value = "同步给技术")
    @PutMapping("/sync/tech/{id}")
    public Result<Boolean> syncTech(@PathVariable Integer id) {
        projectLogService.saveLog(id, "同步给技术");
        return projectService.syncProject(id, 2)
                ? new Result<Boolean>().ok(true) : new Result<Boolean>().error("无可同步的信息");
    }

    @ApiOperation(value = "项目申报")
    @PutMapping("/apply/{id}")
    public Result<Boolean> report(@PathVariable Integer id) {
        return new Result<Boolean>().ok(projectService.report(id));
    }


    @ApiOperation(value = "项目审核(2提交 3通过 4拒绝)")
    @Log(title = "项目审核", businessType = BusinessType.UPDATE)
    @PutMapping("auth/{projectId}/{authState}")
    public Result<Boolean> auth(@PathVariable Integer projectId, @PathVariable Integer authState) {
        String content = "";
        if (authState == 2) {
            notifyService.authProjectMsg(projectId);
            content = "提交审核";
        } else if (authState == 3) {
            notifyService.authProjectFinishMsg(projectId);
            content = "审核通过";
        } else if (authState == 4) {
            notifyService.authProjectFinishMsg(projectId);
            content = "审核拒绝";
        } else {
            throw new ServiceException("authState[" + authState + "]有误");
        }
        projectLogService.saveLog(projectId, content);


        return new Result<Boolean>().ok(projectService.lambdaUpdate().set(Project::getAuthState, authState)
                .eq(Project::getId, projectId).update());
    }
}
