package com.tax.file.web.controller;


import com.tax.file.integration.user.UserClient;
import com.tax.file.model.InvoiceApplyRes;
import com.tax.file.model.TkTaxUser;
import com.tax.file.model.fileManage.ApplyFileReq;
import com.tax.file.model.fileManage.ApplyMonthFileReq;
import com.tax.file.service.api.InvoiceFileService;
import com.tax.file.service.util.UserUtils;
import com.yxcheng.token.common.annotation.LogT;
import com.yxcheng.token.common.controller.BaseClientController;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.CrRpcResult;
import com.yxcheng.token.common.model.JsonResult;
import com.yxcheng.token.utils.UUIDGenerator;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@RestController
@RequestMapping("/fileManage")
public class FileManageController extends BaseClientController {

    @Autowired
    private InvoiceFileService invoiceFileService;
    @Autowired
    private UserClient userClient;

    @RequestMapping(value ="/handApplyFile" , method = RequestMethod.POST)
    @LogT(LogT.LOG_VIEW_PART)
    public JsonResult<List<InvoiceApplyRes>> handApplyFile(@RequestBody ApplyFileReq applyFileReq){
        String timeFlag = UUIDGenerator.generate();
        JsonResult jsonResult = new JsonResult();
        try {
            //1.校验
            CrRpcResult crRpcResult = userClient.isSafe(applyFileReq);
            if(!checkUserSafe(crRpcResult)){
                jsonResult.error(crRpcResult.getCode(),crRpcResult.getMessage());
            } else {
                TkTaxUser taxUser = UserUtils.getTaxUserBytaxno(applyFileReq.getAccessKeyID(), applyFileReq.getTaxno());
                if(null != taxUser && taxUser.getUserStatus().equals(TkTaxUser.TAX_STATUS_NORMAL)) {
                    jsonResult = invoiceFileService.handApplyFile(timeFlag, taxUser, applyFileReq);
                } else {
                    if(null == taxUser ) {
                        jsonResult.errorResponse(ResultEnum.user_taxno_not_exist);
                    } else {
                        jsonResult.errorResponse(ResultEnum.user_taxno_temp_stop);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(timeFlag + " 系统异常:" + e.getMessage());
            jsonResult.errorResponse(ResultEnum.system_error);
        }
        return jsonResult;
    }

    @RequestMapping(value ="/handApplyMonthFile" , method = RequestMethod.POST)
    @LogT(LogT.LOG_VIEW_PART)
    public JsonResult<List<InvoiceApplyRes>> handApplyMonthFile(@RequestBody ApplyMonthFileReq applyMonthFileReq){
        String timeFlag = UUIDGenerator.generate();
        JsonResult jsonResult = new JsonResult();
        try {
            //1.校验
            CrRpcResult crRpcResult = userClient.isSafe(applyMonthFileReq);
            if(!checkUserSafe(crRpcResult)){
                jsonResult.error(crRpcResult.getCode(),crRpcResult.getMessage());
            } else {
                TkTaxUser taxUser = UserUtils.getTaxUserBytaxno(applyMonthFileReq.getAccessKeyID(), applyMonthFileReq.getTaxno());
                if(null != taxUser && taxUser.getUserStatus().equals(TkTaxUser.TAX_STATUS_NORMAL)) {
                    if(taxUser.getIsOnce().equals("2") || applyMonthFileReq.getSignature().equals("twotest")) {
                        jsonResult = invoiceFileService.handApplyMonthFile(timeFlag, taxUser, applyMonthFileReq);
                    } else {
                        jsonResult.errorResponse(ResultEnum.no_inter_error);
                    }
                } else {
                    if(null == taxUser ) {
                        jsonResult.errorResponse(ResultEnum.user_taxno_not_exist);
                    } else {
                        jsonResult.errorResponse(ResultEnum.user_taxno_temp_stop);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
            log.error(timeFlag + " 系统异常:" + e.getMessage());
            jsonResult.errorResponse(ResultEnum.system_error);
        }
        return jsonResult;
    }
}
