package com.dxhy.extend.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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 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.companypool.TicketService;

import lombok.extern.slf4j.Slf4j;

/**
 *
 * 发票统计报表入口
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("modules/ticketSelect")
@Slf4j
public class ExtTicketController extends AbstractController {

    @Resource
    private TicketService ticketService;

    /**
     * 获取税号所属机构信息
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/queryTicket")
    @SysLog("实名客票信息查询")
    public ResponseEntity<R> queryTicket(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = new ArrayList<>();
        List<String> invoiceTypeList = new ArrayList<>();
        if (!"99".equals(paramsMap.get("gfsh")) && paramsMap.get("gfsh") != null && !"".equals(paramsMap.get("gfsh"))) {
            gfshList.add(paramsMap.get("gfsh").toString());
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        if (paramsMap.get("invoiceType") != null && !"".equals(paramsMap.get("invoiceType"))
            && !"99".equals(paramsMap.get("invoiceType"))) {
            String[] invoiceTypeArr = paramsMap.get("invoiceType").toString().split(",");
            for (String uuid : invoiceTypeArr) {
                invoiceTypeList.add(uuid);
                paramsMap.put("list", invoiceTypeList);
            }
        }
        paramsMap.put("gfsh", gfshList);
        if (paramsMap.get("kpksrq") != null && !"".equals(paramsMap.get("kpksrq"))) {
            paramsMap.put("kpksrq", DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("kpksrq").toString())));
        }
        if (paramsMap.get("kpjsrq") != null && !"".equals(paramsMap.get("kpjsrq"))) {
            paramsMap.put("kpjsrq", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("kpjsrq").toString())));
        }
        if (paramsMap.get("cjksyf") != null && !"".equals(paramsMap.get("cjksyf"))) {
            paramsMap.put("cjksyf", DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("cjksyf").toString())));
        }
        if (paramsMap.get("cjjsyf") != null && !"".equals(paramsMap.get("cjjsyf"))) {
            paramsMap.put("cjjsyf", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("cjjsyf").toString())));
        }

        if (paramsMap.get("qsyf") == null && "".equals(paramsMap.get("qsyf")) && !"1".equals(paramsMap.get("qszt"))) {
            paramsMap.put("qsyf", "");
        }
        if ("99".equals(paramsMap.get("isDeduction"))) {
            paramsMap.put("isDeduction", "");
        }
        if ("99".equals(paramsMap.get("invoiceType"))) {
            List<String> list = new ArrayList<>();
            list.add("90");
            list.add("89");
            list.add("92");
            list.add("10");
            list.add("87");
            list.add("88");
            paramsMap.put("list", list);
        }
        int curr = (int)paramsMap.get("curr");
        int size = (int)paramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", ticketService.queryList(paramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
