package com.dxhy.extend.controller;

import static com.dxhy.extend.constants.ExtWebUriMappingConstant.*;

import java.text.SimpleDateFormat;
import java.util.*;

import javax.annotation.Resource;

import com.dxhy.api.model.config.Tax;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.common.constant.DbConstant;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.datasource.config.DynamicContextHolder;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;
import com.dxhy.extend.entity.ExtIndexInvoiceChartStatisticsModel;
import com.dxhy.extend.entity.ExtIndexInvoiceCollectionCountModel;
import com.dxhy.extend.entity.ExtIndexInvoiceMonthInformationModel;
import com.dxhy.extend.entity.ExtIndexInvoiceTaskRemindingModel;
import com.dxhy.extend.service.bb.ExtIndexStatisticsService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@Slf4j
public class ExtIndexController extends AbstractController {
    @Resource
    private ExtIndexStatisticsService indexStatisticsService;

    /**
     * 获取当前用户是否有外贸税号
     *
     * @return 返回
     */
    @PostMapping("/getOrgType")
    public ResponseEntity<R> getOrgType() {
        List<String> gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        if (gfshList.size() > 0) {
            DynamicContextHolder.push(getUserInfo().getDbName() + DbConstant.BUSINESS_READ);
            Integer orgNum = indexStatisticsService.getOrgType(gfshList);
            if (orgNum != null && orgNum > 0) {
                return ResponseEntity.ok(R.ok().put("data", "1"));
            }
            return ResponseEntity.ok(R.ok().put("data", "0"));
        }
        return ResponseEntity.ok(R.ok().put("data", "用户无税号权限"));

    }

    /**
     * 今日发票采集情况
     */
    @PostMapping(URI_INDEX_INVOICE_COLLECTION)
    public ResponseEntity<R> getInvoiceCollectionSituation(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票采集情况,params {}", pramsMap);
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("taxno", taxno);
            List<ExtIndexInvoiceCollectionCountModel> listVO =
                indexStatisticsService.getInvoiceCollectionSituationMap(pramsMap);
            Map<String, Object> map = new HashMap<>(16);
            for (ExtIndexInvoiceCollectionCountModel vo : listVO) {
                map.put(vo.getInvoiceType(), vo.getCountNum());
            }
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日发票采集详情表格
     */

    @PostMapping(URI_INDEX_INVOICE_COLLECTION_DETAIL)
    public ResponseEntity<R> getInvoiceCollectionSituationList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票采集列表,params {}", pramsMap);

        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));
            String invoiceType = (String)pramsMap.get("invoiceType");

            if ("17".equals(invoiceType)) {
                return ResponseEntity.ok(
                    R.ok().put("data", indexStatisticsService.getCustomsCollectionSituationList(pramsMap, curr, size)));
            } else {
                return ResponseEntity.ok(
                    R.ok().put("data", indexStatisticsService.getInvoiceCollectionSituationList(pramsMap, curr, size)));
            }

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日发票签收情况
     */

    @PostMapping(URI_INDEX_INVOICE_SCANNING)
    public ResponseEntity<R> getInvoiceScanningSituation(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票签收情况,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("userName", getUserName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            Map<String, Object> map = indexStatisticsService.getInvoiceScanningSituation(pramsMap);

            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日发票签收详情
     */

    @PostMapping(URI_INDEX_INVOICE_SCANNING_DETAIL)
    public ResponseEntity<R> getInvoiceScanningSituationList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票签收列表,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("userName", getUserName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));

            return ResponseEntity
                .ok(R.ok().put("data", indexStatisticsService.getInvoiceScanningSituationList(pramsMap, curr, size)));

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日发票认证情况
     */

    @PostMapping(URI_INDEX_INVOICE_AUTHENTICATION)
    public ResponseEntity<R> getInvoiceAuthenticationSituation(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票认证情况,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            Map<String, Object> map = indexStatisticsService.getInvoiceAuthenticationSituation(pramsMap);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日退税认证情况
     */
    @PostMapping(URI_INDEX_INVOICE_DRAWBACK)
    public ResponseEntity<R> getDrawbackAuthenticationSituation(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票认证情况,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            Map<String, Object> map = indexStatisticsService.getDrawbackAuthenticationSituation(pramsMap);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 今日发票认证详情
     */
    @PostMapping(URI_INDEX_INVOICE_AUTHENTICATION_DETAIL)
    public ResponseEntity<R> getInvoiceAuthenticationSituationList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-今日发票认证详情,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));

            return ResponseEntity.ok(
                R.ok().put("data", indexStatisticsService.getInvoiceAuthenticationSituationList(pramsMap, curr, size)));

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 本月新增发票信息（新增、认证、发票金额（未税）、发票税额）
     */

    @PostMapping(URI_INDEX_INVOICE_INFORMATION_OF_MONTH)
    public ResponseEntity<R> getInvoiceInformationOfMonth(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-本月新增发票信息,params {}", pramsMap);
        pramsMap.put("userId", getUserId());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            Map<String, Object> map = new HashMap<>(16);
            ExtIndexInvoiceMonthInformationModel vo = indexStatisticsService.getInvoiceInformationOfMonth(pramsMap);
            map.put("xzInvoice", vo.getXzInvoice());
            map.put("yrzInvoice", vo.getYrzInvoice());
            map.put("taxAmountCount", vo.getTaxAmountCount());
            map.put("invoiceAmountCount", vo.getInvoiceAmountCount());
            map.put("drawbackAmountCount", vo.getDrawbackAmountCount());
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 任务提醒
     */

    @PostMapping(URI_INDEX_TASK_REMINDING)
    public ResponseEntity<R> getInvoiceTaskReminding(@RequestParam Map<String, Object> pramsMap) {

        log.info("首页-任务提醒,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            Map<String, Object> map = new HashMap<>(16);
            List<ExtIndexInvoiceTaskRemindingModel> listVO = indexStatisticsService.getInvoiceTaskReminding(pramsMap);
            for (ExtIndexInvoiceTaskRemindingModel vo : listVO) {
                map.put(vo.getWarningType(), vo.getCountNum());
            }
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 本月发票新增、认证成功、认证失败图表(日报)
     */

    @PostMapping(URI_INDEX_CHART_STATISTICS)
    public ResponseEntity<R> getInvoiceChartStatistics(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-本月发票新增、认证成功、认证失败图表,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {

            pramsMap.put("taxno", taxno);
            Map<String, Object> map = new HashMap<>(16);
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);
            String firstday = format.format(c.getTime());
            // 1:本月第一天
            pramsMap.put("firstDay", firstday);
            List<ExtIndexInvoiceChartStatisticsModel> listVO =
                indexStatisticsService.getInvoiceChartStatistics(pramsMap);
            List<Integer> xzInvoiceCountList = new ArrayList<>();
            List<String> dayOfMonthInYearList = new ArrayList<>();
            List<Integer> rzcgInvoiceCountList = new ArrayList<>();
            List<String> rzcgDayOfMonthInYearList = new ArrayList<>();
            for (ExtIndexInvoiceChartStatisticsModel vo : listVO) {
                xzInvoiceCountList.add(vo.getXzInvoiceCount());
                dayOfMonthInYearList.add(vo.getDayOfMonthInYear());
                if (null == vo.getRzcgInvoiceCount()) {
                    rzcgInvoiceCountList.add(0);
                } else {
                    rzcgInvoiceCountList.add(vo.getRzcgInvoiceCount());
                }
                rzcgDayOfMonthInYearList.add(vo.getRzcgDayOfMonthInYear());
            }
            map.put("xzInvoiceCount", xzInvoiceCountList);
            map.put("dayOfMonthInYear", dayOfMonthInYearList);
            map.put("rzcgInvoiceCount", rzcgInvoiceCountList);
            map.put("rzcgDayOfMonthInYear", rzcgDayOfMonthInYearList);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    /**
     * 本年发票新增、认证成功、认证失败图表(月报)
     */

    @PostMapping(URI_INDEX_CHART_STATISTICS_YEAR)
    public ResponseEntity<R> getInvoiceChartStatisticsYear(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-本年发票新增、认证成功、认证失败图表,params {}", pramsMap);
        pramsMap.put("userId", getUserId());
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {

            pramsMap.put("taxno", taxno);
            Map<String, Object> map = new HashMap<>(16);
            String endDate = String.valueOf(pramsMap.get("endDate"));
            endDate = endDate.replaceAll("-", "");
            pramsMap.put("endDate", endDate);
            List<ExtIndexInvoiceChartStatisticsModel> listVO =
                indexStatisticsService.getInvoiceChartStatisticsYear(pramsMap);
            List<Integer> xzInvoiceCountList = new ArrayList<>();
            List<String> dayOfMonthInYearList = new ArrayList<>();
            List<Integer> rzcgInvoiceCountList = new ArrayList<>();
            List<String> rzcgDayOfMonthInYearList = new ArrayList<>();
            for (ExtIndexInvoiceChartStatisticsModel vo : listVO) {

                xzInvoiceCountList.add(vo.getXzInvoiceCount() == null ? 0 : vo.getXzInvoiceCount());
                dayOfMonthInYearList.add(vo.getDayOfMonthInYear());
                rzcgInvoiceCountList.add(vo.getRzcgInvoiceCount() == null ? 0 : vo.getRzcgInvoiceCount());
                rzcgDayOfMonthInYearList.add(vo.getRzcgDayOfMonthInYear());
            }
            map.put("xzInvoiceCount", xzInvoiceCountList);
            map.put("dayOfMonthInYear", dayOfMonthInYearList);
            map.put("rzcgInvoiceCount", rzcgInvoiceCountList);
            map.put("rzcgDayOfMonthInYear", rzcgDayOfMonthInYearList);
            return ResponseEntity.ok(R.ok().put("data", map));
        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    @PostMapping(URI_INDEX_CURRENT_MONTH_AUTH)
    public ResponseEntity<R> getCurrentMonthAuthList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-本月发票认证详情,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {
            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));
            // 获取当前时间
            Date date = new Date();
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM");
            String nowData = dateFormat.format(date).replaceAll("-", "");
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            Calendar c = Calendar.getInstance();
            c.add(Calendar.MONTH, 0);
            c.set(Calendar.DAY_OF_MONTH, 1);
            String firstday = format.format(c.getTime());
            pramsMap.put("nowData", nowData);
            // 本月第一天
            pramsMap.put("startDate", firstday);
            // 获取前月的最后一天
            c = Calendar.getInstance();
            c.add(Calendar.MONTH, 1);
            c.set(Calendar.DAY_OF_MONTH, 0);
            String lastday = format.format(c.getTime());
            pramsMap.put("endDate", lastday);

            return ResponseEntity
                .ok(R.ok().put("data", indexStatisticsService.getCurrentMonthAuthList(pramsMap, curr, size)));

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    @PostMapping(URI_INDEX_RZHYC)
    public ResponseEntity<R> getRzhycList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-认证后异常发票列表,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {

            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));

            return ResponseEntity.ok(R.ok().put("data", indexStatisticsService.getRzhycList(pramsMap, curr, size)));

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    @PostMapping(URI_INDEX_QSHYC)
    public ResponseEntity<R> getQshycList(@RequestParam Map<String, Object> pramsMap) {
        log.info("首页-签收后异常发票列表,params {}", pramsMap);
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> taxno = getTaxno();
        if (taxno != null && taxno.size() > 0) {

            pramsMap.put("taxno", taxno);
            int curr = Integer.parseInt(String.valueOf(pramsMap.get("curr")));
            int size = Integer.parseInt(String.valueOf(pramsMap.get("size")));

            return ResponseEntity.ok(R.ok().put("data", indexStatisticsService.getQshycList(pramsMap, curr, size)));

        } else {
            return ResponseEntity.ok(R.ok().put("data", ""));
        }
    }

    public List<String> getTaxno() {
        List<Tax> listTax = getUserInfo().getOrg();
        List<String> resultList = new ArrayList<>();
        for (Tax ta : listTax) {
            resultList.add(ta.getTaxno());
        }
        return resultList;
    }

}
