package com.jeeplus.test.workContract.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
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.sys.utils.StringUtils;
import com.jeeplus.sys.utils.TicketQueryUtils;
import com.jeeplus.test.program.configuration.projectList.domain.ProgramProjectListInfo;
import com.jeeplus.test.workClientInfo.domain.WorkClientInfo;
import com.jeeplus.test.workClientInfo.domain.dto.WorkClientInfosDto;
import com.jeeplus.test.workClientInfo.service.WorkClientService;
import com.jeeplus.test.workContract.domain.WorkContractInfo;
import com.jeeplus.test.workContract.service.WorkContractService;
import com.jeeplus.test.workContract.service.dto.WorkContractInfoDto;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

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

    @Resource
    private WorkContractService workContractService;

    /**
     * 合同登记列表
     */
    @ApiOperation(value = "合同登记列表")
    @GetMapping(value = "list")
    public ResponseEntity<IPage<WorkContractInfo>> list(WorkContractInfo workClientInfo, Page<WorkContractInfo> page) throws Exception {
        String clientName = workClientInfo.getClientName();
        String[] contractDates = workClientInfo.getContractDates();
        String[] contractAmounts = workClientInfo.getContractAmounts();
        String statusList = workClientInfo.getStatusList();
        String status = workClientInfo.getStatus();
        QueryWrapper<WorkContractInfo> workContractInfoQueryWrapper = QueryWrapperGenerator.buildQueryCondition(workClientInfo, WorkContractInfo.class);
        IPage<WorkContractInfo> list = workContractService.list(page, workContractInfoQueryWrapper, clientName, contractDates, contractAmounts, statusList, status);
        return ResponseEntity.ok (list);
    }

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

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

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

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

    @ApiOperation(value = "关联项目")
    @GetMapping(value = "/relationProjectList")
    public ResponseEntity<IPage<ProgramProjectListInfo>> relationProjectList(@RequestParam String id, Page<ProgramProjectListInfo> page) {
        IPage<ProgramProjectListInfo> infos = workContractService.relationProjectList(id, page);
        return ResponseEntity.ok(infos);
    }
}
