package com.yn.bftl.thirdparty.modules.contract.controller.member;

import com.qiyuesuo.sdk.v2.bean.Contract;
import com.qiyuesuo.sdk.v2.request.CompanyAuthResultRequest;
import com.qiyuesuo.sdk.v2.response.CompanyAuthResult;
import com.yn.bftl.common.modules.company.entity.ContractLock;
import com.yn.bftl.thirdparty.common.exception.YnceErrorException;
import com.yn.bftl.thirdparty.modules.contract.dto.CompanyAuthWithlicenseDTO;
import com.yn.bftl.thirdparty.modules.contract.dto.ContractLockCreateEmployeeDTO;
import com.yn.bftl.thirdparty.modules.contract.dto.ContractLockInviteSubCompanyDTO;
import com.yn.bftl.thirdparty.modules.contract.dto.ContractLockUpdateEmployeeDTO;
import com.yn.bftl.thirdparty.modules.contract.service.ContractLockService;
import com.yn.bftl.thirdparty.modules.contract.util.ContractLockUtil;
import com.yn.bftl.thirdparty.modules.contract.vo.LinkVo;
import com.yn.bftl.thirdparty.modules.contract.vo.StatusVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.Valid;

/**
 * $ContractLockController契约锁控制器接口
 *
 * @author chendehuihuo
 * @date 2022/7/20 17:16
 */
@Validated
@Api(value = "ContractLockController", tags = "客户 - 契约锁操作")
@RestController
@RequestMapping("/contract/lock/member")
public class ContractLockMemberController {

    @Resource
    private ContractLockService contractLockService;

    @ApiOperation(value = "用户 - 获取企业认证地址")
    @GetMapping(value = "/companyAuthLink")
    public LinkVo getCompanyAuthLink() {
        return contractLockService.getCompanyAuthLink();
    }

    @ApiOperation(value = "用户 - 获取企业认证地址(带营业执照)")
    @PostMapping(value = "/companyAuth2Link")
    public LinkVo getCompanyAuth2Link(@Validated CompanyAuthWithlicenseDTO dto) {
        if (dto.getLicense() == null && StringUtils.isBlank(dto.getLicensePath())) {
            throw new YnceErrorException("营业执照不能为空");
        }
        return contractLockService.getCompanyAuth2Link(dto);
    }

    @ApiOperation(value = "用户 - 查询企业认证状态")
    @GetMapping(value = "/companyAuthStatus")
    public StatusVo getCompanyAuthStatus() {
        return contractLockService.getCompanyAuthStatus();
    }

    @ApiOperation(value = "用户 - 发起合同")
    @GetMapping(value = "/send")
    public Boolean send(@RequestParam Long itemId) {
        contractLockService.send(itemId, true);
        return true;
    }

    @ApiOperation(value = "用户 - 获取合同【预签署页面、签署页面】")
    @GetMapping(value = "/show")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "itemId", value = "合同明细ID", dataType = "Long", dataTypeClass = Long.class)
    })
    public LinkVo show(@RequestParam Long itemId) {
        return contractLockService.show(itemId, true);
    }

    @ApiOperation(value = "用户 - 获取合同详情")
    @GetMapping(value = "/details")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "query", required = true, name = "itemId", value = "合同明细ID", dataType = "Long", dataTypeClass = Long.class)
    })
    public Contract getDetails(@RequestParam Long itemId) {
        return contractLockService.getDetails(itemId);
    }

    @ApiOperation("作废合同")
    @PostMapping("/invalid/{id}")
    public boolean invalid(@PathVariable("id") Long contractId, String invalidReason) {
        return contractLockService.invalid(contractId, StringUtils.isEmpty(invalidReason) ? "合同有误" : invalidReason);
    }

    @ApiOperation("收据盖章")
    @GetMapping("/receipt_seal")
    public void generateReceipt() {
        contractLockService.generateReceipt();
    }

    @ApiOperation("作废收据")
    @PostMapping("/receipt_invalid/{id}")
    public boolean invalidReceipt(@PathVariable("id") Long contractId, String invalidReason) {
        return contractLockService.invalid(contractId, StringUtils.isEmpty(invalidReason) ? "收据有误" : invalidReason);
    }

    @ApiOperation("对账单")
    @GetMapping("/statement/{id}")
    public String statement(@PathVariable Long id) {
        return contractLockService.statement(id);
    }

    @ApiOperation("【运维接口】企业认证重新回到第一步")
    @GetMapping("/anew")
    public void anew(Long companyId) {
        contractLockService.anew(companyId);
    }

    @ApiOperation("【运维接口】修改认证中的企业名称、社会信用代码")
    @GetMapping("/update")
    public void update(Long companyId, String name, String code, String legalPerson, Boolean authentication) {
        contractLockService.update(companyId, name, code, legalPerson, authentication);
    }

    @ApiOperation("【运维接口】获取认证中的信息")
    @GetMapping("/contract_lock")
    public ContractLock getContractLock(Long companyId) {
        return contractLockService.contractLock(companyId);
    }

    @ApiOperation(value = "【运维接口】查询契约锁企业认证信息")
    @GetMapping(value = "/companyAuthInfo")
    public CompanyAuthResult getCompanyAuthInfo(@RequestParam String name) {
        CompanyAuthResultRequest request = new CompanyAuthResultRequest();
        request.setCompanyName(name);
        return ContractLockUtil.getInstance().service(request, CompanyAuthResult.class);
    }

    @ApiOperation(value = "子公司-邀请", notes = "返回邀请ID")
    @PostMapping("/sub_company/invite")
    public Long inviteSubCompany(@Valid @RequestBody ContractLockInviteSubCompanyDTO contractLockInviteSubCompanyDTO) {
        return contractLockService.inviteSubCompany(contractLockInviteSubCompanyDTO);
    }

    @ApiOperation(value = "子公司-添加员工")
    @PostMapping("/sub_company/employee/create")
    public void createEmployee(@Valid @RequestBody ContractLockCreateEmployeeDTO contractLockCreateEmployeeDTO) {
        contractLockService.createEmployee(contractLockCreateEmployeeDTO);
    }

    @ApiOperation(value = "子公司-修改员工")
    @PostMapping("/sub_company/employee/update")
    public void updateEmployee(@Valid @RequestBody ContractLockUpdateEmployeeDTO contractLockUpdateEmployeeDTO) {
        contractLockService.updateEmployee(contractLockUpdateEmployeeDTO);
    }
}
