package com.pusong.business.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.dev33.satoken.annotation.SaIgnore;
import com.pusong.business.domain.bo.PsContractInfoBo;
import com.pusong.business.domain.bo.PsContractInfoQueryBo;
import com.pusong.business.domain.bo.PsContractPayBo;
import com.pusong.business.domain.vo.PsContractInfoVo;
import com.pusong.business.service.IPsContractInfoService;
import com.pusong.business.service.IPsContractOperateService;
import com.pusong.business.service.IPsContractPayService;
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.idempotent.annotation.RepeatSubmit;
import com.pusong.common.log.annotation.Log;
import com.pusong.common.log.enums.BusinessType;
import com.pusong.common.mybatis.core.page.PageQuery;
import com.pusong.common.mybatis.core.page.TableDataInfo;
import com.pusong.common.web.core.BaseController;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.springframework.http.MediaType;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 合同操作controller
 *
 * @author wls
 * @date 2024-07-29
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/business/contractOperate")
@SaIgnore
public class PsContractOperateController extends BaseController {

    private final IPsContractPayService payService;
    private final IPsContractOperateService operateService;

    /**
     *  合同签章
     *
     * @param contractCode 合同编码
     */
    @RepeatSubmit()
    @SaCheckPermission("business:contractOperate:sign")
    @GetMapping("/sign")
    public R<Void> getInfo(@NotBlank String contractCode ) {
        operateService.sign(contractCode);
        return R.ok();
    }

    /**
     * 合同添加回款记录
     */
    @RepeatSubmit()
    @SaCheckPermission("business:contractOperate:addPayRecord")
    @PostMapping(value = "/addPayRecord", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> addPayRecord(@RequestPart("files") List<MultipartFile> files,
                                @Validated(AddGroup.class) PsContractPayBo psContractPayBo)throws Exception {
        payService.addPay( files, psContractPayBo);
        return R.ok();
    }

    /**
     * 合同添加退款记录
     */
    @RepeatSubmit()
    @SaCheckPermission("business:contractOperate:addReturnRecord")
    @PostMapping(value = "/addReturnRecord", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> addReturnRecord(@RequestPart(value = "files" ,required = false) List<MultipartFile> files,
                                @Validated(AddGroup.class) PsContractPayBo psContractPayBo)throws Exception {
        payService.returnPay( files, psContractPayBo);
        return R.ok();
    }

    /**
     * 回传合同
     */
    @RepeatSubmit()
    @SaCheckPermission("business:contractOperate:rollBack")
    @PostMapping(value = "/rollBack", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> rollBack(@RequestPart("file") MultipartFile file, @NotBlank String contractCode)throws Exception {
        String s = operateService.rollbackContract(file, contractCode);
        if (s != null){
            return R.fail(s);
        }
        return R.ok();
    }

    /**
     * 确认单
     */
    @RepeatSubmit()
    @SaCheckPermission("business:contractOperate:confirm")
    @PostMapping(value = "/confirm", consumes = MediaType.MULTIPART_FORM_DATA_VALUE)
    public R<Void> confirm(@RequestPart("file") MultipartFile file,@NotBlank String contractCode )throws Exception {
        operateService.confirm( file, contractCode);
        return R.ok();
    }





//
//    /**
//     * 获取合同基本信息详细信息
//     *
//     * @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));
//    }


}
