package com.dxhy.erp.controller;

import com.alibaba.fastjson.JSON;
import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.service.DictdetaServcice;
import com.dxhy.common.utils.R;
import com.dxhy.common.vo.DictdetaModel;
import com.dxhy.erp.dao.SysDeptDao;
import com.dxhy.erp.entity.SysDeptEntity;
import com.dxhy.erp.entity.TDxInvoiceReimburse;
import com.dxhy.erp.model.ReimburseReq;
import com.dxhy.erp.service.TDxInvoiceReimburseService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Value;
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;

import javax.annotation.Resource;
import java.util.List;

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

    @Resource
    SysDeptDao sysDeptDao;
    @Resource
    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);
        SysDeptEntity deptEntity = sysDeptDao.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()));
        }

    }

}
