package com.dxhy.customs.controller;

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

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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.customs.service.CustomsCusLogService;
import com.dxhy.customs.service.CustomsJkszhcxService;

import lombok.extern.slf4j.Slf4j;

/**
 * 海关缴款书综合查询
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/jks")
@Slf4j
public class CustomsJkszhcxController extends AbstractController {

    @Resource
    private CustomsJkszhcxService customsJkszhcxService;

    @Resource
    private CustomsCusLogService customsCusLogService;

    /**
     * 海关缴款书综合查询接口
     *
     * @author 陈刚扬
     * @date 2020/8/10
     * @param pramsMap
     *            入参
     * @return java.lang.Object
     */
    @RequestMapping("/jkszhcx")
    @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");
        try {
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", customsJkszhcxService.selectByPramsMap(pramsMap, curr, size))));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 缴款书票样查询
     *
     * @author 陈刚扬
     * @date 2020/8/10
     * @param map
     *            入参
     * @return org.springframework.http.ResponseEntity<?>
     */
    @PostMapping("/info")
    @SysLog("查询缴款书明细信息")
    public ResponseEntity<?> jksInfo(@RequestBody Map<String, String> map) {
        String customsCode = map.get("customsCode");
        map.put("dbName", getUserInfo().getDbName());
        if (StringUtils.isBlank(customsCode)) {
            return ResponseEntity.ok(R.error("缴款书号码不能为空"));
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", customsJkszhcxService.getCustomsInfo(map)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @RequestMapping("/getCustomsLog")
    @ResponseBody
    @SysLog("缴款书操作记录查询")
    public Object getCustomsLog(@RequestBody Map<String, String> params) {
        try {
            return ResponseEntity.ok(R.ok().put("data", customsCusLogService.getCustomsLog(params)));
        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
