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.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.extend.service.customs.CustomsService;
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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Objects;

/**
 * 海关缴款书--企业票池
 * @author 梁鑫
 */
@RestController
@RequestMapping("customs")
@Slf4j
public class ExtCustomsController extends AbstractController {

    @Resource
    CustomsService customsService;

    /**
     * 海关缴款书票池查询接口-改造自海关缴款书综合查询陈刚扬
     *
     * @author 梁鑫
     * @date 2021/8/12
     * @param pramsMap
     *            入参
     * @return java.lang.Object
     */
    @RequestMapping("/customsList")
    @ResponseBody
    @SysLog("海关缴款书综合查询")
    public Object listFpzhcx(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("business", getUserInfo().getBusiness());
        pramsMap.put("dbName", getUserInfo().getDbName());
        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());
        }
        pramsMap.put("gfsh", gfshList);
        if (pramsMap.get("fillInStartDate") != null && !"".equals(pramsMap.get("fillInStartDate"))) {
            pramsMap.put("fillInStartDate",
                    DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("fillInStartDate").toString())));
        }
        if (pramsMap.get("fillInEndDate") != null && !"".equals(pramsMap.get("fillInEndDate"))) {
            pramsMap.put("fillInEndDate",
                    DateUtils.getNight(DateUtils.strToDate(pramsMap.get("fillInEndDate").toString())));
        }

        if ("1".equals(pramsMap.get("qsStatus"))) {
            if (pramsMap.get("qsDateStartDate") != null && !"".equals(pramsMap.get("qsDateStartDate"))) {
                pramsMap.put("qsDateStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("qsDateStartDate").toString())));
            }
            if (pramsMap.get("qsDateEndDate") != null && !"".equals(pramsMap.get("qsDateEndDate"))) {
                pramsMap.put("qsDateEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("qsDateEndDate").toString())));
            }
            if ("99".equals(pramsMap.get("businessCode"))) {
                pramsMap.put("businessCode", null);
            }
        } else {
            pramsMap.put("qsDateStartDate", null);
            pramsMap.put("qsDateEndDate", null);
            pramsMap.put("businessCode", null);
        }
        if ("1".equals(pramsMap.get("rzhYesorno")) || "2".equals(pramsMap.get("rzhYesorno"))
                || "3".equals(pramsMap.get("rzhYesorno"))) {
            if (pramsMap.get("rzhStartDate") != null && !"".equals(pramsMap.get("rzhStartDate"))) {
                pramsMap.put("rzhStartDate",
                        DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("rzhStartDate").toString())));
            }
            if (pramsMap.get("rzhEndDate") != null && !"".equals(pramsMap.get("rzhEndDate"))) {
                pramsMap.put("rzhEndDate",
                        DateUtils.getNight(DateUtils.strToDate(pramsMap.get("rzhEndDate").toString())));
            }
        } else {
            pramsMap.put("rzhStartDate", null);
            pramsMap.put("rzhEndDate", null);
            pramsMap.put("rzhBelongDate", null);
        }

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

        if("99".equals(pramsMap.get("glzt"))){
            pramsMap.put("glzt", null);
        }
        if("99".equals(pramsMap.get("inAccountStatus"))){
            pramsMap.put("inAccountStatus", null);
        }
        if("99".equals(pramsMap.get("qsStatus"))){
            pramsMap.put("qsStatus", null);
        }
        if("99".equals(pramsMap.get("qsfs"))){
            pramsMap.put("qsfs", null);
        }
        if("99".equals(pramsMap.get("rzhType"))){
            pramsMap.put("rzhType", null);
        }
        if("99".equals(pramsMap.get("rzhYesorno"))){
            pramsMap.put("rzhYesorno", null);
        }

        if(pramsMap.get("imageNumber") != null){
            pramsMap.put("imageId",pramsMap.get("imageNumber"));
        }

        if(pramsMap.get("snVoucherNumber") != null){
            pramsMap.put("snVoucherNumber",pramsMap.get("snVoucherNumber"));
        }

        if(pramsMap.get("accountPeriod") != null ){
            if(pramsMap.get("accountPeriod").toString().length() > 7) {
                String accountPeriod = pramsMap.get("accountPeriod").toString().substring(0, 7);
                pramsMap.put("accountPeriod", accountPeriod);
            }else{
                pramsMap.put("accountPeriod", pramsMap.get("accountPeriod"));
            }
        }

        if (pramsMap.get("postingTimeStart") != null && !"".equals(pramsMap.get("postingTimeStart"))) {
            pramsMap.put("postingTimeStart",
                    DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("postingTimeStart").toString())));
        }
        if (pramsMap.get("postingTimeEnd") != null && !"".equals(pramsMap.get("postingTimeEnd"))) {
            pramsMap.put("postingTimeEnd",
                    DateUtils.getNight(DateUtils.strToDate(pramsMap.get("postingTimeEnd").toString())));
        }
        String companyCode = (String) pramsMap.get("companyCode");
        if (StringHelper.isNotBlank(companyCode) && "99".equals(companyCode)){
            pramsMap.put("companyCodes",getUserInfo().getCompanyCodes());
            log.info("组织编码为====>{}",pramsMap.get("companyCodes"));
        }

        log.info("请求列表的参数为:{}",pramsMap);

        try {
            return ResponseEntity.ok(Objects
                    .requireNonNull(R.ok().put("data", customsService.selectByPramsMap(pramsMap, curr, size))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
