package cn.com.ty.lift.business.contract.controller;

import cn.com.ty.lift.business.contract.dao.entity.Contracts;
import cn.com.ty.lift.business.contract.dao.entity.model.request.ContractRequest;
import cn.com.ty.lift.business.contract.dao.entity.model.response.ContractResponse;
import cn.com.ty.lift.business.contract.dao.entity.model.response.ContractsHistoryResponse;
import cn.com.ty.lift.business.contract.service.ContractHistoryService;
import cn.com.ty.lift.business.contract.service.ContractService;
import cn.com.ty.lift.business.framework.util.MessageUtils;
import cn.com.ty.lift.common.verify.Val;
import cn.com.ty.lift.common.verify.Validation;
import cn.com.ty.lift.common.verify.Validator;
import cn.com.xwy.boot.web.dto.RestResponse;
import com.baomidou.mybatisplus.core.metadata.IPage;
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.annotation.Resource;
import java.util.Map;

/**
 * @author bieao
 * @date 2019/12/07
 * @description 合同管理控制层
 */
@RestController
@RequestMapping("contract")
public class ContractController {

    @Resource
    private ContractService contractService;

    @Resource
    private ContractHistoryService contractHistoryService;

    /**
     * @param request 合同列表查询条件
     * @return RestResponse 合同分页列表结果
     * @description 查询合同列表
     * @date 2019/12/7 11:53 AM
     */
    @PostMapping("list")
    @Validation(fields = {"mtCompanyId"})
    public RestResponse list(@Val @RequestBody ContractRequest request) {
        IPage<ContractResponse> page = contractService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }

    /**
     * @param request 合同操作记录列表查询条件
     * @return RestResponse 合同操作记录分页列表结果
     * @description 查询合同操作记录列表
     * @date 2019/12/7 11:53 AM
     */
    @PostMapping("history/list")
    @Validation(fields = {"contractId"})
    public RestResponse historyList(@RequestBody ContractRequest request) {
        IPage<ContractsHistoryResponse> page = contractHistoryService.list(request);
        return RestResponse.success(page, MessageUtils.get("msg.query.success"));
    }


    /**
     * @param request 新增合同数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 新增合同
     * @date 2019/12/7 3:31 PM
     */
    @PostMapping("add")
    @Validation(fields = {"mtCompanyId", "projectName", "projectAddress"})
    public RestResponse add(@Val @RequestBody ContractRequest request) {
        Contracts contracts = request.getContracts();
        Validator.valid(contracts, "liftNum", "moneys", "type", "givenDate", "givenUser", "starDate", "endDate");
        boolean result = contracts.getStarDate().isAfter(contracts.getEndDate());
        if (result) return RestResponse.success(null, MessageUtils.get("msg.time.error"));
        return contractService.add(request);
    }

    /**
     * @param request 续签合同数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 续签合同
     * @date 2020/5/3 10:46 上午
     */
    @PostMapping("renew")
    @Validation(fields = {"mtCompanyId", "contractId"})
    public RestResponse renew(@Val @RequestBody ContractRequest request) {
        Contracts contracts = request.getContracts();
        Validator.valid(contracts, "liftNum", "moneys", "type", "givenDate", "givenUser", "starDate", "endDate");
        boolean result = contracts.getStarDate().isAfter(contracts.getEndDate());
        if (result) return RestResponse.success(null, MessageUtils.get("msg.time.error"));
        return contractService.renew(request);
    }


    /**
     * @param request 修改合同数据项
     * @return 1.成功, 0.失败, 消息描述
     * @description 合同变更
     * @date 2019/12/7 3:31 PM
     */
    @PostMapping("modify")
    @Validation(fields = {"mtCompanyId", "id"})
    public RestResponse modify(@Val @RequestBody ContractRequest request) {
        return contractService.modify(request);
    }

    /**
     * @param request 合同复核
     * @return 1.成功, 0.失败, 消息描述
     * @description 合同复核
     * @date 2020/2/11 12:55 下午
     */
    @PostMapping("check")
    @Validation(fields = {"id"})
    public RestResponse check(@Val @RequestBody ContractRequest request) {
        return contractService.check(request);
    }

    /**
     * @param request 合同id
     * @return RestResponse 合同详情
     * @description 查询合同详情
     * @date 2019/12/6 4:46 PM
     */
    @PostMapping("detail")
    @Validation(fields = {"id"})
    public RestResponse detail(@Val @RequestBody ContractRequest request) {
        Map<String,Object> resultMap = contractService.detail(request.getId());
        return RestResponse.success(resultMap, MessageUtils.get("msg.query.detail.success"));
    }

}
