package com.pusong.business.controller;

import java.util.List;

import cn.dev33.satoken.annotation.SaIgnore;
import com.pusong.business.domain.bo.PsContractInfoQueryBo;
import com.pusong.business.domain.bo.PsInvoiceBo;
import com.pusong.business.domain.vo.PsContractBusinessVo;
import com.pusong.business.domain.vo.PsSalaryContractVo;
import com.pusong.business.domain.vo.ServiceScheduleVo;
import com.pusong.business.service.IPsContractBusinessService;
import com.pusong.common.core.validate.QueryGroup;
import lombok.RequiredArgsConstructor;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.*;
import cn.dev33.satoken.annotation.SaCheckPermission;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import com.pusong.common.idempotent.annotation.RepeatSubmit;
import com.pusong.common.log.annotation.Log;
import com.pusong.common.web.core.BaseController;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.pusong.common.core.domain.R;
import com.pusong.common.core.validate.AddGroup;
import com.pusong.common.core.validate.EditGroup;
import com.pusong.common.log.enums.BusinessType;
import com.pusong.common.excel.utils.ExcelUtil;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.domain.bo.PsContractInfoBo;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.common.mybatis.core.page.TableDataInfo;

import javax.management.Query;

/**
 * 合同基本信息
 *
 * @author wls
 * @date 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/contractInfo")
@SaIgnore
public class PsContractInfoController extends BaseController {

    private final IPsContractInfoService psContractInfoService;
    private final IPsContractBusinessService businessService;


    /**
     * 生成合同
     */
    @SaCheckPermission("business:customInfo:addContract")
    @Log(title = "生成合同", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/create")
    public R<Long> add(@Validated(AddGroup.class) @RequestBody PsContractInfoBo bo) {
        return R.ok(psContractInfoService.createContract(bo,2));
    }
    /**
     * 暂存合同
     */
    @SaCheckPermission("business:customInfo:addContract")
    @Log(title = "暂存合同", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping("/hold")
    public R<Long> hold( @RequestBody PsContractInfoBo bo) {
        return R.ok(psContractInfoService.createContract(bo,1));
    }
    /**
     * 预览合同
     */
    @SaCheckPermission("business:customInfo:addContract")
    @Log(title = "预览合同", businessType = BusinessType.INSERT)
    @PostMapping("/preview")
    public R<Long> preview( @RequestBody PsContractInfoBo bo) {
        return R.ok(psContractInfoService.preview(bo));
    }
    /**
     * 根据客户id查询暂存的合同详情信息
     */
    @SaCheckPermission("business:contractInfo:add")
    @Log(title = "根据客户id查询暂存的合同详情信息", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/queryContract")
    public R<PsContractInfoVo> queryContract(@NotNull Long customerId) {
        return R.ok(psContractInfoService.queryInfoByCustomId(customerId));
    }

    /**
     * 分页查询合同列表
     */
    @SaCheckPermission("business:contractInfo:LIST")
    @GetMapping("/list")
    public TableDataInfo<PsContractInfoVo> list(PsContractInfoQueryBo bo, PageQuery pageQuery) {
        return psContractInfoService.queryPageList(bo, pageQuery);
    }

    /**
     * 使用客户id 查询分页查询合同列表
     */
    @SaCheckPermission("business:contractInfo:LIST")
    @GetMapping("/queryPageListByCustomId")
    public TableDataInfo<PsContractInfoVo> queryPageListByCustomId(@RequestParam Long customId, PageQuery pageQuery) {
        return psContractInfoService.queryPageListByCustomId(customId, pageQuery);
    }

    /**
     * 根据合同编码查询合同信息
     */
    @SaCheckPermission("business:contractInfo:list")
    @Log(title = "根据合同编码查询合同信息", businessType = BusinessType.INSERT)
    @GetMapping("/queryContractByCode")
    public R<PsContractInfoVo> queryContractByCode(@NotBlank String contractCode) {
        return R.ok(psContractInfoService.queryContractByCode(contractCode));
    }

    /**
     * 修改合同
     */
    @SaCheckPermission("business:contractList:edit")
    @Log(title = "根据合同编码查询合同信息", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping("/edit")
    public R<Boolean> updateContractByCode(@Validated(EditGroup.class) @RequestBody PsContractInfoBo bo) {
        return R.ok(psContractInfoService.updateContractByCode(bo));
    }

    /**
     * 作废合同
     */
    @SaCheckPermission("business:contractList:cancellation")
    @Log(title = "作废合同", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @GetMapping("/cancellation")
    public R<Boolean> cancellation(@NotBlank String contractCode, String desc) {
        return R.ok(psContractInfoService.cancellation(contractCode,desc));
    }

    /**
     * 重启合同
     */
    @SaCheckPermission("business:contractList:restart")
    @Log(title = "重启合同", businessType = BusinessType.DELETE)
    @RepeatSubmit()
    @GetMapping("/restart")
    public R<Boolean> restart(@NotBlank String contractCode) {
        return R.ok(psContractInfoService.restart(contractCode));
    }

    /**
     * 根据合同编码查询服务信息
     */
    @SaCheckPermission("business:contractInfo:list")
    @Log(title = "根据合同编码查询合同信息", businessType = BusinessType.INSERT)
    @GetMapping("/queryServiceByCode")
    public R<List<PsContractBusinessVo>> queryServiceByCode(@NotBlank String contractCode, @RequestParam(required = false) Long serviceCompanyId) {
        return R.ok(businessService.selectBusinessList(List.of(contractCode), serviceCompanyId));
    }



    /**
     * 开发票（开发票操作）
     */
    @SaCheckPermission("business:contractInfo:list")
    @Log(title = "合同开发票", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/invoice")
    public R<Void> invoice(@NotBlank String contractCode) {
        psContractInfoService.invoice(contractCode);
        return R.ok();
    }

    /**
     * 开发票（开发票操作）
     */
    @SaCheckPermission("business:contractInfo:list")
    @Log(title = "合同开发票", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @GetMapping("/invoice_new")
    public R<Void> invoiceNew(@Validated(AddGroup.class)  PsInvoiceBo psInvoiceBo) {
        psContractInfoService.invoice(psInvoiceBo);
        return R.ok();
    }


    /**
     * 免费派单
     * @param businessType 服务类别
     * @param businessProject 服务项目
     * @return
     */
    @RepeatSubmit()
    @Log(title = "免费派单")
    @SaCheckPermission("business:task:list")
    @GetMapping("/freeWork")
    public R<Void> freeWork(@NotNull String serviceCompanyId, @NotNull String businessType,@NotNull String businessProject) {
        psContractInfoService.freeWork(serviceCompanyId, businessType, businessProject);
        return R.ok();
    }
    /**
     * 根据合同编码查询服务进度
     * @param contractCode 合同编码
     * @return
     */
    @RepeatSubmit()
    @Log(title = "根据合同编码查询服务进度")
    @SaCheckPermission("business:task:list")
    @GetMapping("/queryScheduleByCode")
    public R<ServiceScheduleVo> queryScheduleByCode(@NotNull String contractCode, @NotNull Long serviceCompanyId) {
        return R.ok(psContractInfoService.queryScheduleByCode(contractCode, serviceCompanyId));
    }

//
//    /**
//     * 获取合同基本信息详细信息
//     *
//     * @param id 主键
//     */
//    @SaCheckPermission("business:contractInfo:query")
//    @GetMapping("/{id}")
//    public R<PsContractInfoVo> getInfo(@NotNull(message = "主键不能为空")
//                                     @PathVariable Long id) {
//        return R.ok(psContractInfoService.queryById(id));
//    }
//

//
//    /**
//     * 修改合同基本信息
//     */
//    @SaCheckPermission("business:contractInfo:edit")
//    @Log(title = "合同基本信息", businessType = BusinessType.UPDATE)
//    @RepeatSubmit()
//    @PutMapping()
//    public R<Void> edit(@Validated(EditGroup.class) @RequestBody PsContractInfoBo bo) {
//        return toAjax(psContractInfoService.updateByBo(bo));
//    }


}
