package com.dxhy.extend.controller;

import com.dxhy.api.model.config.Tax;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.extend.service.bb.InvoiceCertificationReportService;
import com.dxhy.extend.service.tjbb.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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 javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.*;

import static org.springframework.web.bind.annotation.RequestMethod.POST;

/**
 * 发票统计报表入口
 *
 * @author 张宏 @date：2019年5月11日
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("report")
@Slf4j
public class ExtReportController extends AbstractController {

    @Resource
    private IExtFprzybService fprzybService;

    @Resource
    private IExtRzjgqdService rzjgqdService;

    @Resource
    private IExtQstjbService qstjbService;
    @Resource
    private IExtQssbService qssbService;
    @Resource
    private IExtFprzhrService fprzhrService;

    @Resource
    private InvoiceCertificationReportService invoiceCertificationReportService;

    /**
     * 认证人员统计
     *
     * @param pramsMap
     *            入参
     * @return 返回
     */
    @RequestMapping("/listFprzUser")
    @SysLog("认证人员统计")
    public ResponseEntity<R> listFprzUser(@RequestBody Map<String, Object> pramsMap) {
        String gfsh = (String)pramsMap.get("gfsh");
        String rzhr = (String)pramsMap.get("rzhr");
        String skssq = (String)pramsMap.get("skssq");
        String kpksrq = (String)pramsMap.get("kpksrq");
        String kpjsrq = (String)pramsMap.get("kpjsrq");
        String rzksrq = (String)pramsMap.get("rzksrq");
        String rzjsrq = (String)pramsMap.get("rzjsrq");
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        Map<String, Object> map = new HashMap<>(16);
        map.put("curr", curr);
        map.put("size", size);
        map.put("business", getUserInfo().getBusiness());
        map.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        map.put("gfsh", gfshList);
        if (StringUtils.isNotBlank(rzhr)) {
            map.put("rzhr", rzhr);
        }
        if (StringUtils.isNotBlank(skssq)) {
            map.put("skssq", skssq);
        }
        if (StringUtils.isNotBlank(kpksrq) && StringUtils.isNotBlank(kpjsrq)) {
            map.put("kpksrq", kpksrq);
            map.put("kpjsrq", kpjsrq);
        }
        if (StringUtils.isNotBlank(rzksrq) && StringUtils.isNotBlank(rzjsrq)) {
            map.put("rzksrq", rzksrq);
            map.put("rzjsrq", rzjsrq);
        }
        map.put("company", pramsMap.get("company"));
        map.put("menuId", pramsMap.get("menuId"));
        try {
            return ResponseEntity.ok(R.ok().put("data", fprzhrService.invoiceStatByRzhr(map)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 发票认证月票查询
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listFprzyb")
    @SysLog("发票认证月票查询")
    public ResponseEntity<R> listFprzyb(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        String gfmc = "";
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            if(pramsMap.get("gfsh").toString().length()!=15&&pramsMap.get("gfsh").toString().length()!=18){
                return ResponseEntity.ok(R.error("税号长度不正确，请传15或18位税号！"));
            }
            data.put("gfsh", pramsMap.get("gfsh"));
            List<Tax> org = getUserInfo().getOrg();
            if (org != null && org.size() > 0) {
                for (Tax tax : org) {
                    if (tax.getTaxno().equals(pramsMap.get("gfsh"))) {
                        gfmc = tax.getTaxname();
                    }
                }
            }
            data.put("gfmc", gfmc);
        } else {
            return ResponseEntity.ok(R.error("请选择对应的购方税号！"));
        }
        if (!"99".equals(pramsMap.get("rznf")) && pramsMap.get("rznf") != null && !"".equals(pramsMap.get("rznf"))) {
            data.put("rznf", pramsMap.get("rznf"));
        } else {
            return ResponseEntity.ok(R.error("认证年份不能为空！"));
        }
        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));
        data.put("dklx",pramsMap.get("dklx"));
        try {
            return ResponseEntity.ok(R.ok().put("data", fprzybService.selectByGfshAndRznf(data)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 认证结果清单查询
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listRzjgqd")
    @SysLog("认证结果清单查询")
    public ResponseEntity<R> listRzjgqd(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);

        data.put("dbName", getUserInfo().getDbName());
        String gfmc = "";
        if (!"99".equals(pramsMap.get("gfsh")) && pramsMap.get("gfsh") != null && !"".equals(pramsMap.get("gfsh"))) {
            data.put("gfsh", pramsMap.get("gfsh"));
            List<Tax> org = getUserInfo().getOrg();
            if (org != null && org.size() > 0) {
                for (Tax tax : org) {
                    if (tax.getTaxno().equals(pramsMap.get("gfsh"))) {
                        gfmc = tax.getTaxname();
                    }
                }
            }
            data.put("gfmc", gfmc);
        } else {
            return ResponseEntity.ok(R.error("请选择对应的购方税号！"));
        }

        if (pramsMap.get("skssq") != null && !"".equals(pramsMap.get("skssq"))) {
            data.put("skssq", pramsMap.get("skssq"));
        } else {
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMM");
            data.put("skssq", dateFormat.format(new Date()));
        }
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));
        data.put("dklx",pramsMap.get("dklx"));

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

    }

    /**
     * 发票签收统计表查询
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listQstjb")
    @SysLog("发票签收统计表查询")
    public ResponseEntity<R> listQstjb(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);
        data.put("business", getUserInfo().getBusiness());
        data.put("dbName", getUserInfo().getDbName());
        data.put("businessCode", pramsMap.get("businessCode"));
        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", ""));
            }
        }
        data.put("gfsh", gfshList);

        if (pramsMap.get("qszt") != null && !"".equals(pramsMap.get("qszt")) && !"99".equals(pramsMap.get("qszt"))) {
            data.put("qszt", pramsMap.get("qszt"));
        }
        if (pramsMap.get("qsfs") != null && !"".equals(pramsMap.get("qsfs")) && !"99".equals(pramsMap.get("qsfs"))) {
            data.put("qsfs", pramsMap.get("qsfs"));
        }

        if (pramsMap.get("qsrqq") != null && !"".equals(pramsMap.get("qsrqq"))) {
            data.put("qsrqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsrqq").toString())));
        }
        if (pramsMap.get("qsrqz") != null && !"".equals(pramsMap.get("qsrqz"))) {
            data.put("qsrqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsrqz").toString())));
        }

        data.put("xfmc", pramsMap.get("xfmc"));
        data.put("qsr", pramsMap.get("qsr"));

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");

        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));

        //发票号码20位判断
        String invoiceNo = pramsMap.get("fphm").toString();
        if (!StringHelper.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                data.put("fphm", invoiceNo);
            }else {
                data.put("eleInvoiceNo", invoiceNo);
            }
        }

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

    }

    /**
     * 认证发票汇总报表
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listRzfphz")
    @SysLog("认证发票汇总报表")
    public ResponseEntity<R> listRzfphz(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        if (!gfshList.isEmpty()) {
            data.put("gfsh", gfshList);
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }

        data.put("rznf", pramsMap.get("rznf"));
        data.put("company", pramsMap.get("company"));
        data.put("menuId", pramsMap.get("menuId"));
        data.put("dklx",pramsMap.get("dklx"));
        try {
            return ResponseEntity.ok (R.ok().put("data", fprzybService.selectRzfphzByRznf(data)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 发票签收失败报表查询
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listQssb")
    @SysLog("发票签收失败报表查询")
    public ResponseEntity<R> listQssb(@RequestBody Map<String, Object> pramsMap) {

        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        data.put("businessCode", pramsMap.get("businessCode"));
        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", ""));
            }
        }
        data.put("gfsh", gfshList);

        if (pramsMap.get("qsrqq") != null && !"".equals(pramsMap.get("qsrqq"))) {
            data.put("qsrqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsrqq").toString())));
        }
        if (pramsMap.get("qsrqz") != null && !"".equals(pramsMap.get("qsrqz"))) {
            data.put("qsrqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsrqz").toString())));
        }

        data.put("xfmc", pramsMap.get("xfmc"));
        data.put("qsr", pramsMap.get("qsr"));

        String invoiceNo = pramsMap.get("fphm").toString();
        if (!StringHelper.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                data.put("fphm", invoiceNo);
            }else {
                data.put("eleInvoiceNo", invoiceNo);
                data.put("fphm", "");
            }
        }

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

    /**
     * 已签收未认证查询入口
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listYqswrz")
    @SysLog("已签收未认证查询入口")
    public ResponseEntity<R> listYqswrz(@RequestBody Map<String, Object> pramsMap) {
        Map<String, Object> data = new HashMap<>(16);

        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        data.put("businessCode", pramsMap.get("businessCode"));
        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", ""));
            }
        }
        data.put("gfsh", gfshList);

        if (pramsMap.get("qsfs") != null && !"".equals(pramsMap.get("qsfs")) && !"99".equals(pramsMap.get("qsfs"))) {
            data.put("qsfs", pramsMap.get("qsfs"));
        }

        if (pramsMap.get("qsrqq") != null && !"".equals(pramsMap.get("qsrqq"))) {
            data.put("qsrqq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsrqq").toString())));
        }
        if (pramsMap.get("qsrqz") != null && !"".equals(pramsMap.get("qsrqz"))) {
            data.put("qsrqz", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsrqz").toString())));
        }

        data.put("xfmc", pramsMap.get("xfmc"));
        data.put("qsr", pramsMap.get("qsr"));
        Object fphm = pramsMap.get("fphm");

        String invoiceNo = pramsMap.get("fphm").toString();
        if (!StringHelper.isBlank(invoiceNo)) {
            if(invoiceNo.length()==8){
                data.put("fphm", invoiceNo);
            }else {
                data.put("eleInvoiceNo", invoiceNo);
            }
        }

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

    }

    /**
     * 导出数据-认证结果清单
     *
     * @param request
     *            request
     * @param response
     *            response
     * @return 返回
     */
    @RequestMapping("/authResultListExport")
    @SysLog("导出数据-认证结果清单")
    public void authResultListExport(HttpServletRequest request, HttpServletResponse response) {
        String gfsh = request.getParameter("gfsh");
        String skssq = request.getParameter("skssq");
        Map<String, Object> pramsMap = new HashMap<>(16);

        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        pramsMap.put("gfsh", gfshList);
        pramsMap.put("skssq", skssq);

        pramsMap.put("currentDate", DateUtils.getStringDate());

        try {
//            rzjgqdService.exportPdf(pramsMap, response);
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
        }
    }

    @RequestMapping("/rzInfoByXf")
    @SysLog("按销方统计认证情况")
    public ResponseEntity<R> rzInfoByXf(@RequestBody Map<String, Object> pramsMap) {
        String gfsh = (String)pramsMap.get("gfsh");
        String rzzt = (String)pramsMap.get("rzzt");
        String skssq = (String)pramsMap.get("skssq");
        String kpksrq = (String)pramsMap.get("kpksrq");
        String kpjsrq = (String)pramsMap.get("kpjsrq");
        String rzksrq = (String)pramsMap.get("rzksrq");
        String rzjsrq = (String)pramsMap.get("rzjsrq");
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        Map<String, Object> map = new HashMap<>(16);
        map.put("curr", curr);
        map.put("size", size);
        map.put("business", getUserInfo().getBusiness());
        map.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        map.put("gfsh", gfshList);
        if (StringUtils.isNotBlank(rzzt)) {
            map.put("rzzt", rzzt);
        }
        if (StringUtils.isNotBlank(skssq)) {
            map.put("skssq", skssq);
        }
        if (StringUtils.isNotBlank(kpksrq) && StringUtils.isNotBlank(kpjsrq)) {
            map.put("kpksrq", kpksrq);
            map.put("kpjsrq", kpjsrq);
        }
        if ("1".equals(rzzt) && StringUtils.isNotBlank(rzksrq) && StringUtils.isNotBlank(rzjsrq)) {
            map.put("rzksrq", rzksrq);
            map.put("rzjsrq", rzjsrq);
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", fprzhrService.invoiceStatByRzhr(map)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    @SysLog("今日头条认证汇总报表")
    @RequestMapping(value = "/invoiceCertificationReport", method = {POST})
    public ResponseEntity<R> list(@RequestBody Map<String, Object> params) {

        String gfsh = (String)params.get("gfsh");
        params.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        params.put("gfsh", gfshList);
        try {
            return ResponseEntity
                .ok(R.ok().put("data", invoiceCertificationReportService.queryInvoiceCertification(params)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
