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.companypool.SaleCountService;
import com.dxhy.extend.service.companypool.SaleSignService;
import lombok.extern.slf4j.Slf4j;
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;

/**
 *
 * 增值税申报数据
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("modules/saleCount")
@Slf4j
public class ExtSaleCountController extends AbstractController {

    @Resource
    private SaleCountService saleCountService;

    /**
     * 销项汇总表（按税率）
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySaleCountByRate")
    @SysLog("销项汇总税率查询")
    public ResponseEntity<R> querySaleCountByRate(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = 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 (!"99".equals(paramsMap.get("invoiceType"))) {
            if ("01".equals(paramsMap.get("invoiceType"))) {
                paramsMap.put("fplx", "0");
            } else if ("04".equals(paramsMap.get("invoiceType"))) {
                paramsMap.put("fplx", "2");
            } else if ("10".equals(paramsMap.get("invoiceType"))) {
                paramsMap.put("fplx", "51");
            } else if ("11".equals(paramsMap.get("invoiceType"))) {
                paramsMap.put("fplx", "41");
            }else if("32".equals(paramsMap.get("invoiceType")) || "31".equals(paramsMap.get("invoiceType")) ){
                paramsMap.put("fplx", paramsMap.get("invoiceType"));
            }
        } else {
            paramsMap.put("fplx", "99");
        }
        paramsMap.put("gfsh", gfshList);
        try {
            return ResponseEntity.ok(R.ok().put("data", saleCountService.queryRateList(paramsMap)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项汇总表（按项目）
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySaleCountByItem")
    @SysLog("销项汇总项目查询")
    public ResponseEntity<R> querySaleCountByItem(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = 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", ""));
            }
        }
        paramsMap.put("gfsh", gfshList);
        int curr = (int)paramsMap.get("curr");
        int size = (int)paramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", saleCountService.queryItemList(paramsMap, curr, size)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 销项汇总表（按项目）
     *
     * @param paramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/querySaleInvoiceByItem")
    @SysLog("销项汇总发票查询")
    public ResponseEntity<R> querySaleInvoiceByItem(@RequestBody Map<String, Object> paramsMap) {
        paramsMap.put("dbName", getUserInfo().getDbName());

        List<String> gfshList = 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", ""));
            }
        }
        paramsMap.put("gfsh", gfshList);
        if (paramsMap.get("beginDate") != null && !"".equals(paramsMap.get("beginDate"))) {
            paramsMap.put("beginDate",
                DateUtils.getMorning(DateUtils.strToDate(paramsMap.get("beginDate").toString())));
        }
        if (paramsMap.get("endDate") != null && !"".equals(paramsMap.get("endDate"))) {
            paramsMap.put("endDate", DateUtils.getNight(DateUtils.strToDate(paramsMap.get("endDate").toString())));
        }

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

}
