package com.ruoyi.project.business.controller.contract;

import java.util.ArrayList;
import java.util.List;
import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.project.business.domain.bo.TqContractBo;
import com.ruoyi.project.business.domain.vo.contract.TqContractDto;
import com.ruoyi.project.business.domain.vo.contract.TqContractVo;
import com.ruoyi.project.business.mapper.flow.FlowMapper;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.framework.aspectj.lang.annotation.Log;
import com.ruoyi.framework.aspectj.lang.enums.BusinessType;
import com.ruoyi.project.business.domain.TqContract;
import com.ruoyi.project.business.service.ITqContractService;
import com.ruoyi.framework.web.controller.BaseController;
import com.ruoyi.framework.web.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.framework.web.page.TableDataInfo;

import static com.ruoyi.project.business.enums.AuditFlowEnum.*;

/**
 * 合同列表Controller
 * 
 * @author ruoyi
 * @date 2024-08-13
 */
@RestController
@RequestMapping("/business/contract")
public class TqContractController extends BaseController
{
    @Resource
    private ITqContractService tqContractService;

    @Resource
    private FlowMapper flowMapper;

    /**
     * 查询合同列表列表
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:list')")
    @GetMapping("/list")
    public TableDataInfo list(TqContract tqContract)
    {
        startPage();
        List<TqContract> list = tqContractService.selectTqContractList(tqContract);
        return getDataTable(list);
    }

    @GetMapping("/listBo")
    public TableDataInfo list(TqContractBo tqContractBo)
    {
        startPage();
        List<TqContractVo> list = tqContractService.selectTqContractBoList(tqContractBo);
        return getDataTable(list);
    }
    @GetMapping("/list-app")
    public TableDataInfo listApp(TqContractBo tqContractBo)
    {
        startPage();
        //app只查询审核中的
        List<String> status = new ArrayList<>();
        status.add("0");
        tqContractBo.setApproveStatus(status);
        List<TqContractVo> list = tqContractService.selectTqContractBoListApp(tqContractBo);
        return getDataTable(list);
    }
    /**
     * 导出合同列表列表
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:export')")
    @Log(title = "合同列表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TqContractBo tqContractBo)
    {
        List<TqContractVo> list = tqContractService.selectTqContractBoList(tqContractBo);
        ExcelUtil<TqContractVo> util = new ExcelUtil<TqContractVo>(TqContractVo.class);
        util.exportExcel(response, list, "合同列表数据");
    }

     /**
      * @Author Chenj
      * @Description    导出合同模板
      * @Date 9:09 2024/8/22
      **/
    // @PreAuthorize("@ss.hasPermi('business:contract:export')")
    @Log(title = "合同列表", businessType = BusinessType.EXPORT)
    @PostMapping("/exportContract/{ids}")
    public void export(HttpServletResponse response, @PathVariable Long[] ids)
    {
        Long id = ids[0];
        tqContractService.exportContract(id);
    }

    /**
     * 获取合同列表详细信息
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(tqContractService.selectTqContractById(id));
    }

    @GetMapping(value = "/getContractById/{id}")
    public AjaxResult getContractById(@PathVariable("id") Long id)
    {
        return success(tqContractService.getContractById(id));
    }

    /**
     * 新增合同列表
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:add')")
    @Log(title = "合同列表", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TqContractDto tqContractDto)
    {
        return toAjax(tqContractService.insertTqContract(tqContractDto));
    }

    /**
     * 修改合同列表
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:edit')")
    @Log(title = "合同列表", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TqContractDto tqContractDto)
    {
        return toAjax(tqContractService.updateTqContract(tqContractDto));
    }

    @Log(title = "合同列表", businessType = BusinessType.UPDATE)
    @PutMapping("/editFlow")
    public AjaxResult editFlow(@RequestBody TqContractDto tqContractDto)
    {
        //付款方式是信用证  || 付款比例不是是30/70  走财务流程
        if("1".equals(tqContractDto.getPaymentMethod()) || !"1".equals(tqContractDto.getPaymentRatio())){
            if("200".equals(flowMapper.getStatus(Long.valueOf(CONTRACT_FINANCE_YES.getValue()),tqContractDto.getContractNo()))){
                return toAjax(tqContractService.updateTqContract(tqContractDto));
            }
        }
        //否则走非财务流程
        if("200".equals(flowMapper.getStatus(Long.valueOf(CONTRACT_FINANCE_NO.getValue()),tqContractDto.getContractNo()))){
            return toAjax(tqContractService.updateTqContract(tqContractDto));
        }
        return toAjax(1);
    }



    /**
     * 删除合同列表
     */
    // @PreAuthorize("@ss.hasPermi('business:contract:remove')")
    @Log(title = "合同列表", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(tqContractService.deleteTqContractByIds(ids));
    }


    /**
     * 周、月签约、排产、发货统计
     */
    @GetMapping(value = "/contractSigningStatistic")
    public AjaxResult contractSigningStatistic(TqContractDto params)
    {
        return success(tqContractService.contractSigningStatistic(params));
    }
}
