package com.chushang.inspection.project.controller;

import com.chushang.common.core.exception.utils.AssertUtil;
import com.chushang.common.core.validator.Create;
import com.chushang.common.core.validator.Update;
import com.chushang.common.core.web.AjaxResult;
import com.chushang.common.log.annotation.SysLog;
import com.chushang.common.log.enums.BusinessType;
import com.chushang.common.mybatis.page.CommonParam;
import com.chushang.common.mybatis.utils.PageResult;
import com.chushang.inspection.project.dto.ContractDTO;
import com.chushang.inspection.project.dto.ProjectDTO;
import com.chushang.inspection.project.po.WrkProject;
import com.chushang.inspection.project.po.WrkProjectContract;
import com.chushang.inspection.project.po.WrkProjectPayment;
import com.chushang.inspection.project.query.ProjectQuery;
import com.chushang.inspection.project.service.WrkProjectContractService;
import com.chushang.inspection.project.service.WrkProjectPaymentService;
import com.chushang.inspection.project.service.WrkProjectService;
import com.chushang.security.annotation.RequiresPermissions;
import lombok.extern.slf4j.Slf4j;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;

/**
 * @auther: zhao
 * @date: 2024/6/15 11:22
 */
@Slf4j
@RestController
@RequestMapping(value = "/project")
public class WrkProjectController {

    @Resource
    WrkProjectService projectService;
    @Resource
    WrkProjectContractService contractService;
    @Resource
    WrkProjectPaymentService paymentService;

    /**
     * 获取项目详细信息
     */
    @GetMapping(value = "/{projectId}")
    @RequiresPermissions("inspection:project:info")
    public AjaxResult get(@PathVariable Long projectId){
        return AjaxResult.success(projectService.getProject(projectId));
    }
    /**
     * 获取项目列表
     */
    @GetMapping("/list")
    @RequiresPermissions("inspection:project:list")
    public AjaxResult list(WrkProject project)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        PageResult pageResult = projectService.pageList(project, commonParam);
        return AjaxResult.success(pageResult);
    }

    /**
     * 新增项目 -- 暂存 + 提交审核
     */
    @PostMapping(value = "/save")
    @RequiresPermissions("inspection:project:save")
    @SysLog(value = "项目",businessType = BusinessType.INSERT)
    public AjaxResult save(@RequestBody @Validated(Create.class) ProjectDTO project){
        return AjaxResult.success(projectService.save(project));
    }

    /**
     * 修改 项目
     */
    @PostMapping(value = "/update")
    @RequiresPermissions("inspection:project:save")
    @SysLog(value = "项目",businessType = BusinessType.UPDATE)
    public AjaxResult update(@RequestBody @Validated(Update.class) ProjectDTO project){
        Long projectId = project.getProjectId();
        AssertUtil.invalidate(null == projectId, "修改项目时, 项目id 不能为空");
        return AjaxResult.success(projectService.update(project));
    }

    /**
     * 删除项目
     */
    @DeleteMapping(value = "/{projectId}")
    @RequiresPermissions("inspection:project:del")
    @SysLog(value = "项目",businessType = BusinessType.DELETE)
    public AjaxResult delProject(@PathVariable Long projectId)
    {
        return AjaxResult.success(projectService.delete(projectId));
    }

    /**
     * 合同新增
     * 续签
     */
    @PostMapping(value = "/contract/save")
    @SysLog(value = "合同", businessType = BusinessType.INSERT)
    @RequiresPermissions("inspection:contract:save")
    public AjaxResult save(@RequestBody @Validated(Create.class) ContractDTO contract)
    {
        Long projectId = contract.getProjectId();
        AssertUtil.invalidate(null == projectId, "新增合同时, 项目id 不能为空");
        return AjaxResult.success(projectService.saveContract(contract));
    }

    /**
     * 修改 合同
     */
    @PostMapping(value = "/contract/update")
    @SysLog(value = "合同", businessType = BusinessType.UPDATE)
    @RequiresPermissions("inspection:contract:update")
    public AjaxResult update(@RequestBody @Validated(Update.class) ContractDTO contract){
        Long contractId = contract.getContractId();
        AssertUtil.invalidate(null == contractId, "修改合同时, 合同id 不能为空");
        return AjaxResult.success(projectService.updateContract(contract));
    }

    /**
     * 删除 合同
     */
    @DeleteMapping(value = "/contract/{contractId}")
    @RequiresPermissions("inspection:contract:del")
    @SysLog(value = "合同", businessType = BusinessType.DELETE)
    public AjaxResult del(@PathVariable Long contractId){
        projectService.deleteContract(contractId);
        return AjaxResult.success();
    }

    /**
     * 获取 项目合同列表
     * 根据 项目Id 获取
     */
    @GetMapping(value = "/contract/list")
    @RequiresPermissions("inspection:contract:list")
    public AjaxResult list(WrkProjectContract contract)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        return AjaxResult.success(contractService.pageList(contract, commonParam));
    }


    /**
     * 财务数据
     * 根据 项目Id 获取
     */
    @GetMapping(value = "/payMentData")
    @RequiresPermissions("inspection:contract:list")
    public AjaxResult payMentData(WrkProjectPayment query)
    {
        CommonParam commonParam = CommonParam.buildPageRequest();
        return AjaxResult.success(paymentService.payMentData(query, commonParam));
    }
    /**
     * 待审核 合同 列表 -->
     */

//    /**
//
//    /**
//     * 根据岗位编号获取详细信息
//     */
//    @RequiresPermissions("system:post:query")
//    @GetMapping(value = "/{postId}")
//    public AjaxResult getInfo(@PathVariable Long postId)
//    {
//        return AjaxResult.success(postService.getById(postId));
//    }
    /**
     * 项目数据 展示
     */
    @GetMapping(value = "/project/data")
    @RequiresPermissions("inspection:project:info")
    public AjaxResult projectData(ProjectQuery projectQuery)
    {
        return AjaxResult.success(projectService.projectData(projectQuery));
    }

    /**
     * 项目数据导出
     * @param projectQuery 查询条件
     */
    @SysLog(value = "项目数据", businessType = BusinessType.EXPORT)
    @GetMapping(value = "/project/data/export")
//    @RequiresPermissions("inspection:project:export")
    public AjaxResult projectDataExport(ProjectQuery projectQuery)
    {
        return AjaxResult.success("项目数据导出成功, 请在后台任务中查看, 任务id: ["+  projectService.projectDataExport(projectQuery) +"]");
    }



}
