package com.dxhy.extend.controller;

import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.extend.service.pool.ExtInvoicePoolService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.http.ResponseEntity;
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.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 票池采集-lxx
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("modules/invoicepool")
@Slf4j
public class ExtInvoicePoolController extends AbstractController {

    @Resource
    private ExtInvoicePoolService extInvoicePoolService;

    /**
     * 增值税发票采集
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listAddTaxInvoice")
    @SysLog("增值税发票采集")
    public ResponseEntity<R> listAddTaxInvoice(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);
        if (pramsMap.get("cjksyf") != null && !"".equals(pramsMap.get("cjksyf"))) {
            pramsMap.put("cjksyf", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("cjksyf").toString())));
        }
        if (pramsMap.get("cjjsyf") != null && !"".equals(pramsMap.get("cjjsyf"))) {
            pramsMap.put("cjjsyf", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("cjjsyf").toString())));
        }
        if (pramsMap.get("kpksrq") != null && !"".equals(pramsMap.get("kpksrq"))) {
            pramsMap.put("kpksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("kpksrq").toString())));
        }
        if (pramsMap.get("kpjsrq") != null && !"".equals(pramsMap.get("kpjsrq"))) {
            pramsMap.put("kpjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("kpjsrq").toString())));
        }
        if (pramsMap.get("skssq") != null && !"".equals(pramsMap.get("skssq")) && "1".equals(pramsMap.get("rzzt"))) {
            pramsMap.put("skssq", pramsMap.get("skssq").toString().replace("-", ""));
        } else {
            pramsMap.put("skssq", "");
        }
        if (pramsMap.get("qsyf") == null && "".equals(pramsMap.get("qsyf")) && !"1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsyf", "");
        }
        if (pramsMap.get("inAccountMonth") == null && "".equals(pramsMap.get("inAccountMonth"))
            && !"1".equals(pramsMap.get("inAccountStatus"))) {
            pramsMap.put("inAccountMonth", "");
        }

        //入账属期
        if(ObjectUtils.isNotEmpty(pramsMap.get("fprzsq"))){
            pramsMap.put("fprzsq", pramsMap.get("fprzsq").toString().replace("-", ""));
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", extInvoicePoolService.listAddTax(pramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }


    /**
     * 进项票池摊销记录
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/amortizeRecord")
    @SysLog("进项票池摊销记录")
    public ResponseEntity<R> amortizeRecord(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());
        try {
            return ResponseEntity.ok(R.ok().put("data", extInvoicePoolService.amortizeRecord(pramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 其他票据采集
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listOtherInvoice")
    @SysLog("其他发票采集")
    public ResponseEntity<R> listOtherInvoice(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        List<String> invoiceTypeList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);
        if (pramsMap.get("cjksyf") != null && !"".equals(pramsMap.get("cjksyf"))) {
            pramsMap.put("cjksyf", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("cjksyf").toString())));
        }
        if (pramsMap.get("cjjsyf") != null && !"".equals(pramsMap.get("cjjsyf"))) {
            pramsMap.put("cjjsyf", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("cjjsyf").toString())));
        }
        if (pramsMap.get("kpksrq") != null && !"".equals(pramsMap.get("kpksrq"))) {
            pramsMap.put("kpksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("kpksrq").toString())));
        }
        if (pramsMap.get("kpjsrq") != null && !"".equals(pramsMap.get("kpjsrq"))) {
            pramsMap.put("kpjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("kpjsrq").toString())));
        }
        if (pramsMap.get("qsyf") == null && "".equals(pramsMap.get("qsyf")) && !"1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsyf", "");
        }
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", extInvoicePoolService.listOther(pramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项票池
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listSaleInvoice")
    @SysLog("销项票池采集")
    public ResponseEntity<R> listSaleInvoice(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            gfshList.add(pramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("taxno", gfshList);
        if (pramsMap.get("cjksyf") != null && !"".equals(pramsMap.get("cjksyf"))) {
            pramsMap.put("cjksyf", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("cjksyf").toString())));
        }
        if (pramsMap.get("cjjsyf") != null && !"".equals(pramsMap.get("cjjsyf"))) {
            pramsMap.put("cjjsyf", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("cjjsyf").toString())));
        }
        if (pramsMap.get("kpksrq") != null && !"".equals(pramsMap.get("kpksrq"))) {
            pramsMap.put("kpksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("kpksrq").toString())));
        }
        if (pramsMap.get("kpjsrq") != null && !"".equals(pramsMap.get("kpjsrq"))) {
            pramsMap.put("kpjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("kpjsrq").toString())));
        }

        if (pramsMap.get("qsyf") == null && "".equals(pramsMap.get("qsyf")) && !"1".equals(pramsMap.get("qszt"))) {
            pramsMap.put("qsyf", "");
        }
        if (pramsMap.get("inAccountMonth") == null && "".equals(pramsMap.get("inAccountMonth"))
            && !"1".equals(pramsMap.get("inAccountStatus"))) {
            pramsMap.put("inAccountMonth", "");
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", extInvoicePoolService.listSaleInvoice(pramsMap, curr, size))));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项票池优惠政策
     *
     * @return 返回
     */
    @RequestMapping("/incentiveFlagList")
    @SysLog("销项票池优惠政策查询")
    public ResponseEntity<R> incentiveFlagList() {
        try {
            return ResponseEntity
                .ok(R.ok().put("data", extInvoicePoolService.incentiveFlagList(getUserInfo().getDbName())));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
