package com.jeeplus.business.workContractInfo.controller;

import cn.hutool.core.util.ObjectUtil;
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.business.workContractInfo.domain.JyWorkContractInfo;
import com.jeeplus.business.workContractInfo.service.JyWorkContractService;
import com.jeeplus.business.workContractInfo.service.dto.JyWorkContractInfoDto;
import com.jeeplus.common.utils.ResponseUtil;
import com.jeeplus.core.query.QueryWrapperGenerator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

//import com.jeeplus.sys.utils.TicketQueryUtils;

@Slf4j
@Api(tags ="嘉溢合同登记")
@RestController
@RequestMapping(value = "/jy/workContract/workContractInfo")
public class JyWorkContractController {

    @Resource
    private JyWorkContractService jyWorkContractService;

    /**
     * 合同登记列表
     */
    @ApiOperation(value = "合同登记列表")
    @GetMapping(value = "list")
    public ResponseEntity<IPage<JyWorkContractInfo>> list(JyWorkContractInfo jyWorkContractInfo, Page<JyWorkContractInfo> page) throws Exception {
        String primaryLinkman = jyWorkContractInfo.getClientName();
        String[] contractDates = jyWorkContractInfo.getContractDates();
        String[] contractAmounts = jyWorkContractInfo.getContractAmounts();
        String[] createDates= jyWorkContractInfo.getCreateDates();
        String statusList = jyWorkContractInfo.getStatusList();
        String filedType = jyWorkContractInfo.getFiledType();
        String name=jyWorkContractInfo.getName();
        String createById=jyWorkContractInfo.getCreateById();
        String no=jyWorkContractInfo.getNo();
        List contractTypeList = jyWorkContractInfo.getContractTypes();
        QueryWrapper<JyWorkContractInfo> workContractInfoQueryWrapper = QueryWrapperGenerator.buildQueryCondition(jyWorkContractInfo, JyWorkContractInfo.class);
        if (ObjectUtil.isNotEmpty(jyWorkContractInfo) && StringUtils.isNotBlank(jyWorkContractInfo.getCreateById())) {
            workContractInfoQueryWrapper.eq("a.create_by_id", jyWorkContractInfo.getCreateById());
        }
        IPage<JyWorkContractInfo> list = jyWorkContractService.list(page, workContractInfoQueryWrapper, primaryLinkman, contractDates,createDates, contractAmounts,name,createById, filedType,no,contractTypeList);
        return ResponseEntity.ok (list);
    }

    /**
     * 合同新增/修改
     */
    @ApiOperation(value = "合同新增/修改")
    @PostMapping(value = "save")
    public ResponseEntity<String> save(@RequestBody JyWorkContractInfoDto workContractInfoDto) throws Exception{
        String id =jyWorkContractService.saveInfo(workContractInfoDto);
        return ResponseUtil.newInstance().add("businessTable", "jy_work_contract_info").add("businessId", id).ok ("操作成功");
    }

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

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


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

    /**
     * 归档送审
     */
    @ApiOperation(value = "归档送审")
    @PostMapping(value = "saveContract")
    public ResponseEntity<String> saveContract(@RequestBody JyWorkContractInfoDto workContractInfoDto) throws Exception{
        String id =jyWorkContractService.saveContract(workContractInfoDto);
        return ResponseUtil.newInstance().add("businessTable", "jy_work_contract_info").add("businessId", id).ok ("操作成功");
    }

    /**
     * 删除
     */
    @ApiOperation(value = "根据id删除")
    @GetMapping(value = "/deleteById")
    public ResponseEntity<String> deleteById(@RequestParam String id){
        String s = jyWorkContractService.deleteById(id);
        return ResponseEntity.ok(s);
    }

}
