package com.jwipc.sys.controller;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.jwipc.api.dto.ESignFileResposeDTO;
import com.jwipc.api.esign.SignFileCreateCallBackLogic;
import com.jwipc.constant.SignProcessType;
import com.jwipc.exception.DaoException;
import com.jwipc.sys.entity.DigitalSealBusinessDetail;
import com.jwipc.sys.service.IDigitalSealBusinessConfigService;
import com.jwipc.sys.service.IDigitalSealBusinessDetailService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/pc/sign/file/rest")
@RequiredArgsConstructor
@Slf4j
public class SignFileCreateController {
    private final IDigitalSealBusinessDetailService digitalSealBusinessDetailService;


    private final List<SignFileCreateCallBackLogic> signFileCreateCallBackLogics;

    @PostMapping("create")
    public List<ESignFileResposeDTO> create(Long businessId, String businessTypeCode,Integer templateType) {
        log.info("{}", businessId);
        for (SignFileCreateCallBackLogic service : signFileCreateCallBackLogics) {
            final SignProcessType signProcessType = service.getProcessType();
            if (businessTypeCode.equals(signProcessType.getBusinessNo()) ) {
                 return service.processor(businessId,templateType);
            }
        }
        throw new DaoException("未找到业务处理逻辑");
    }

    @PostMapping("createCheck")
    public ResponseEntity createCheck(Long businessId, String businessTypeCode) {
        DigitalSealBusinessDetail digitalSealBusinessDetail = digitalSealBusinessDetailService.getOne(Wrappers.lambdaQuery(DigitalSealBusinessDetail.class).eq(DigitalSealBusinessDetail::getBusinessId,businessId).eq(DigitalSealBusinessDetail::getBusinessTypeCode,businessTypeCode).ne(DigitalSealBusinessDetail::getSignStatus,7).last(" limit 1"));
        return ResponseEntity.ok(ObjectUtil.isNotNull(digitalSealBusinessDetail));
    }

    @PostMapping("signFileDelete")
    public ResponseEntity signFileDelete(Long businessId, String businessTypeCode) {
        digitalSealBusinessDetailService.remove(Wrappers.lambdaQuery(DigitalSealBusinessDetail.class).eq(DigitalSealBusinessDetail::getBusinessId,businessId).eq(DigitalSealBusinessDetail::getBusinessTypeCode,businessTypeCode));
        return ResponseEntity.ok().build();
    }
}
