package com.jeeplus.test.cw.contractRegistration.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.test.cw.contractRegistration.domain.ContractInfo;
import com.jeeplus.test.cw.contractRegistration.service.ContractInfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author: 王强
 * @create: 2022-11-08 10:52
 **/
@RestController
@Api(tags ="财务合同登记管理")
@RequestMapping(value = "/contract/registration")
public class ContractInfoController {

    @Autowired
    private ContractInfoService service;

    /**
     * 列表查询
     * @param info
     * @return
     */
    @ApiOperation(value = "列表查询")
    @PreAuthorize("hasAnyAuthority('cw_work_contract_info:list')")
    @GetMapping("/list")
    public ResponseEntity<IPage<ContractInfo>> list(ContractInfo info, Page<ContractInfo> page) throws Exception {
        QueryWrapper<ContractInfo> wrapper = QueryWrapperGenerator.buildQueryCondition(info, ContractInfo.class);
        IPage<ContractInfo> list = service.list(page,info,wrapper);
        return ResponseEntity.ok(list);
    }

    /**
     * 合同登记新增/修改    cw:workContract:add
     */
    @ApiOperation(value = "合同登记新增/修改")
    @PreAuthorize("hasAnyAuthority('cw:workContract:add')")
    @PostMapping(value = "save")
    public ResponseEntity<String> save(@RequestBody ContractInfo info) throws Exception{
        String id = service.saveInfo(info);
        return ResponseUtil.newInstance().add("businessTable", "cw_work_contract_info").add("businessId", id).ok ("操作成功");
    }

    /**
     * 合同登记信息删除
     */
    @ApiOperation(value = "合同登记删除")
    @GetMapping(value = "remove")
    public String remove(@RequestParam String id) {
        return service.removeById(id);
    }

    /**
     * 合同登记查询
     */
    @ApiOperation(value = "合同登记查询")
    @GetMapping(value = "findById")
    public ContractInfo findById(@RequestParam String id) {
        return service.findById(id);
    }

    /**
     * 合同登记查询附件信息
     */
    @ApiOperation(value = "合同登记查询")
    @GetMapping(value = "findFileInfoById")
    public ContractInfo findFileInfoById(@RequestParam String id) {
        return service.findFileInfoById(id);
    }

    /**
     * 合同登记查询
     */
    @ApiOperation(value = "根据归档id查询")
    @GetMapping(value = "findByContractId")
    public ContractInfo findByContractId(@RequestParam String id) {
        return service.findByContractId(id);
    }

    /**
     * 根据id修改状态status
     */
    @ApiOperation(value = "根据id修改状态status")
    @PostMapping(value = "updateStatusById")
    public void updateStatusById(@RequestBody ContractInfo info) {
        service.updateStatusById(info);
    }

    /**
     * 根据id修改合同实际金额
     */
    @ApiOperation(value = "根据id修改合同实际金额")
    @PostMapping(value = "updateInfo")
    public void updateInfo(@RequestBody ContractInfo info) {
        service.updateInfo(info);
    }

    @ApiOperation(value = "根据客户id查询关联的项目")
    @GetMapping("/getByClientId")
    public ResponseEntity<List<ContractInfo>> getByClientId(String contractId, ContractInfo contractInfo) {
        List<ContractInfo> list = service.getByClientId(contractId,contractInfo);
        return ResponseEntity.ok(list);
    }
}
