package com.jkcredit.invoice.controller.business;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jkcredit.invoice.annotation.LoginRequired;
import com.jkcredit.invoice.annotation.annotationdes.AuthenticationInterceptor;
import com.jkcredit.invoice.mapper.customer.CustomerRecMapper;
import com.jkcredit.invoice.model.entity.customer.CustomerRec;
import com.jkcredit.invoice.model.entity.user.User;
import com.jkcredit.invoice.model.entity.waybill.NoCarCompanyManger;
import com.jkcredit.invoice.model.vo.OperatorEditConfirmVo;
import com.jkcredit.invoice.model.vo.OperatorEditVo;
import com.jkcredit.invoice.model.vo.OperatorSendCodeVo;
import com.jkcredit.invoice.service.lowerservice.CheckHasAuthRole;
import com.jkcredit.invoice.service.nocar.NoCarCompanyMangerOper;
import com.jkcredit.invoice.util.RespR;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * All rights Reserved, Designed By www.erayt.com
 *
 * @author mashengyi
 * @version V4.1
 * @Title rcs41
 * @Package com.jkcredit.invoice.controller.business
 * @Description 无车企业开票人管理
 * @date 2024/4/4 下午1:11
 * @Copyright: 2024 www.erayt.com Inc. All rights reserved.
 * 注意：本内容仅限于杭州时代银通软件股份有限公司内部传阅，禁止外泄以及用于其他的商业目的
 **/

@Api(tags = "无车企业开票人管理")
@RestController
@RequestMapping(value = {"/customerCompany"})
public class CustomerCompanyManagerOperController {

    @Autowired
    NoCarCompanyMangerOper noCarCompanyMangerOper;

    @Autowired
    private CheckHasAuthRole checkHasAuthRole;
    @Autowired
    CustomerRecMapper customerRecMapper;
    /**
     * 新增上传企业开票经办人
     *
     * @return 经办人集合
     */
    @PostMapping("/detailAdd")
    @ApiOperation(value = "新增上传企业开票经办人信息", notes = "新增上传企业开票经办人信息")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR CustomerCompanyManagerDetailAdd(@RequestBody @Validated NoCarCompanyManger noCarCompanyManger) {
        return noCarCompanyMangerOper.companyDetailAdd(noCarCompanyManger);
    }


    /**
     * 新增上传证明文件
     *
     * @return 经办人集合
     */
    @PostMapping("/operatorEvidenceAdd")
    @ApiOperation(value = "新增上传证明文件", notes = "新增上传证明文件")
    @LoginRequired(role = AuthenticationInterceptor.AUTH_ADMIN)
    public RespR CustomerCompanyManageroperatorEvidenceAdd(@RequestBody NoCarCompanyManger noCarCompanyManger) {
        if (!checkHasAuthRole.checkMessage(noCarCompanyManger.getOperatorEvidenceContractFileName(), noCarCompanyManger.getOperatorEvidenceBase64Str())) {
            return new RespR(false, "证明文件上传失败，请查看数据是否维护准确");
        }
        return noCarCompanyMangerOper.operatorEvidenceAdd(noCarCompanyManger);
    }


    /**
     * 分页查询企业开票经办人
     *
     * @param page   参数集
     * @param user 查询参数列表
     * @return 用户集合
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询企业开票经办人", notes = "分页查询企业开票经办人")
    @LoginRequired
    public RespR getCustomerCompanyManagerDetailPage(Page page, NoCarCompanyManger noCarCompanyManger, User user) {
        checkHasAuthRole.checkCustomerRole(user, noCarCompanyManger.getCustId());
        return new RespR<>(noCarCompanyMangerOper.getCustomerCompanyManagerDetailPage(page, noCarCompanyManger));
    }


    /**
     * @param noCarCompanyManger
     * @return
     */
    @PostMapping("/noCarCompanyMangerQuery")
    @ApiOperation(value = "企业开票经办人信息查询", notes = "企业开票经办人信息查询")
    @LoginRequired
    public RespR getCustomerCompanyManagerDetail(@RequestBody NoCarCompanyManger noCarCompanyManger, User user) {
        return noCarCompanyMangerOper.noCarCompanyMangerQuery(noCarCompanyManger);
    }

    /**
     * @param operatorEditVo
     * @return
     */
    @PostMapping("/updateNoCarCompanyManger")
    @ApiOperation(value = "修改企业开票经办人", notes = "修改企业开票经办人")
    @LoginRequired
    public RespR updateNoCarCompanyManger(@RequestBody OperatorEditVo operatorEditVo) {
        return noCarCompanyMangerOper.operatorEdit(operatorEditVo);
    }

    /**
     * @param operatorEditConfirmVo
     * @return
     */
    @PostMapping("/updateNoCarCompanyMangerConfirm")
    @ApiOperation(value = "确认修改企业开票经办人", notes = "确认修改企业开票经办人")
    @LoginRequired
    public RespR updateNoCarCompanyMangerConfirm(@RequestBody OperatorEditConfirmVo operatorEditConfirmVo) {
        return noCarCompanyMangerOper.operatorEditConfirm(operatorEditConfirmVo);
    }
    /**
     * @param operatorSendCodeVo
     * @return
     */
    @PostMapping("/sendCode")
    @ApiOperation(value = "发送验证码", notes = "发送验证码")
    @LoginRequired
    public RespR sendCode(@RequestBody OperatorSendCodeVo operatorSendCodeVo) {
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCompanyNum(operatorSendCodeVo.getBuyerCode());
        List<CustomerRec> customerRecList = customerRecMapper.selectByCompanyNum(customerRec);
        operatorSendCodeVo.setBuyerCode(customerRecList.get(0).getCompanyReferencenum());
        return noCarCompanyMangerOper.operatorSendCode(operatorSendCodeVo, customerRec);
    }
    /**
     * @param operatorSendCodeVo
     * @return
     */
    @PostMapping("/confirmCode")
    @ApiOperation(value = "确认验证码", notes = "确认验证码")
    @LoginRequired
    public RespR confirmCode(@RequestBody OperatorSendCodeVo operatorSendCodeVo) {
        CustomerRec customerRec = new CustomerRec();
        customerRec.setCompanyNum(operatorSendCodeVo.getBuyerCode());
        List<CustomerRec> customerRecList = customerRecMapper.selectByCompanyNum(customerRec);
        operatorSendCodeVo.setBuyerCode(customerRecList.get(0).getCompanyReferencenum());
        return noCarCompanyMangerOper.operatorValidCode(operatorSendCodeVo, customerRec);
    }
}
