package com.dxhy.erp.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.api.model.config.sys.SysEnterpriseConfigDto;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.utils.R;
import com.dxhy.erp.dao.SysEnterpriseConfigErpDao;
import com.dxhy.api.model.invoice.TDxInvoiceReimburse;
import com.dxhy.erp.model.ReimburseReq;
import com.dxhy.erp.service.TDxInvoiceReimburseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
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;

/**
 * @author Gy Chen
 * @date 2021/3/2
 */
@RestController
@RequestMapping("/reimbursement")
@Slf4j
public class ReimbursementController {

    @Autowired
    SysEnterpriseConfigErpDao enterpriseConfigErpDao;
    @Autowired
    TDxInvoiceReimburseService tDxInvoiceReimburseService;

    @PostMapping("/reimbursementSync")
    public ResponseEntity<R> reimbursementSync(@RequestBody String reimbursementData) {
        if (StringUtils.isBlank(reimbursementData)) {
            return ResponseEntity.ok(R.error("票据信息不可为空"));
        }
        log.info("接收票据:{}", reimbursementData);
        ReimburseReq reimburseReq = JSON.parseObject(reimbursementData, ReimburseReq.class);
        /*        String msg = checkData(reimburseReq);
        if (StringUtils.isNotBlank(msg)){
            return ResponseEntity.ok(R.error(msg));
        }*/
        DynamicContextHolder.push(DbConstant.BASICS_READ);
        SysEnterpriseConfigDto deptEntity = enterpriseConfigErpDao.getOrgType(reimburseReq.getGfTaxNo());
        if (deptEntity == null) {
            return ResponseEntity.ok(R.error("购方税号未配置"));
        }
        TDxInvoiceReimburse reimburse = tDxInvoiceReimburseService.invoiceHandle(reimburseReq, deptEntity);
        if ("1".equals(reimburse.getQsStatus())) {
            return ResponseEntity.ok(R.ok(reimburse.getNotes()));
        } else {
            return ResponseEntity.ok(R.error(reimburse.getNotes()));
        }

    }

}
