package com.dxhy.extend.controller;

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

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

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.bb.ViolationLogService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author Gy Chen
 * @date 2021/3/9
 */
@Controller
@RequestMapping("/risk")
@Slf4j
public class RiskInvoiceController extends AbstractController {
    @Resource
    ViolationLogService violationLogService;

    @PostMapping("/supplier")
    @SysLog("供应商风险查询")
    public Object selectRiskSupplier(@RequestBody Map<String, Object> pramsMap) {
        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        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);
        data.put("taxNo", pramsMap.get("taxNo"));
        data.put("taxName", pramsMap.get("taxName"));
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        // 设置日期格式
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
        String date = df.format(new Date());
        data.put("currentTime", date);
        try {
            return ResponseEntity.ok(R.ok().put("data", violationLogService.selectRiskSupplier(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/supplier/info")
    @SysLog("供应商风险查询")
    public Object selectRiskSupplierInfo(@RequestBody Map<String, Object> pramsMap) {
        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        List<String> gfshList = new ArrayList<>();
        if (StringUtils.isBlank((String)pramsMap.get("taxNo"))) {
            return ResponseEntity.ok(R.error("税号不能为空"));
        }
        data.put("taxNo", pramsMap.get("taxNo"));
        try {
            return ResponseEntity.ok(R.ok().put("data", violationLogService.selectRiskSupplierInfo(data)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/riskInvoice")
    @SysLog("风险发票查询")
    public Object selectRiskInvoice(@RequestBody Map<String, Object> pramsMap) {
        Map<String, Object> data = new HashMap<>(16);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        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("invoiceDateStart") != null && !"".equals(pramsMap.get("invoiceDateStart"))) {
            data.put("invoiceDateStart",
                DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("invoiceDateStart").toString())));
        }
        if (pramsMap.get("invoiceDateEnd") != null && !"".equals(pramsMap.get("invoiceDateEnd"))) {
            data.put("invoiceDateEnd",
                DateUtils.getNight(DateUtils.strToDate(pramsMap.get("invoiceDateEnd").toString())));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("invoiceType"))
            && !"99".equals((String)pramsMap.get("invoiceType"))) {
            data.put("invoiceType", pramsMap.get("invoiceType"));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("invoiceStatus"))
            && !"99".equals((String)pramsMap.get("invoiceStatus"))) {
            data.put("invoiceStatus", pramsMap.get("invoiceStatus"));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("glzt")) && !"99".equals(pramsMap.get("glzt"))) {
            data.put("glzt", pramsMap.get("glzt"));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("rzzt")) && !"99".equals(pramsMap.get("rzzt"))) {
            data.put("rzzt", pramsMap.get("rzzt"));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("rzlx")) && !"99".equals(pramsMap.get("rzlx"))) {
            data.put("rzlx", pramsMap.get("rzlx"));
        }
        if (StringUtils.isNotBlank((String)pramsMap.get("fxlx")) && !"99".equals(pramsMap.get("fxlx"))) {
            data.put("fxlx", pramsMap.get("fxlx"));
        }
        data.put("searchKey", pramsMap.get("searchKey"));
        data.put("invoiceSource",pramsMap.get("invoiceSource"));
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", violationLogService.selectRiskInvoice(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
    @PostMapping("/enterprise/info")
    @SysLog("触发企业管控规则明细")
    public Object selectEnterpriseInfo(@RequestBody Map<String, Object> pramsMap) {
        Map<String, Object> data = new HashMap<>(5);
        data.put("dbName", getUserInfo().getDbName());
        data.put("business", getUserInfo().getBusiness());
        if (StringUtils.isBlank((String)pramsMap.get("uuid"))) {
            return ResponseEntity.ok(R.error("发票标识不能为空"));
        }
        data.put("uuid", pramsMap.get("uuid"));
        try {
            return ResponseEntity.ok(R.ok().put("data", violationLogService.selectEnterpriseInfo(data)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
