package com.dxhy.extend.controller;

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.PostMapping;
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.R;
import com.dxhy.extend.model.ExtCautionAbnormalReqVO;
import com.dxhy.extend.model.ExtCautionExpirationReqVO;
import com.dxhy.extend.model.ExtCautionMaximalReqVO;
import com.dxhy.extend.service.caution.IExtCautionExpirationService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author peipei.li
 * @date 2019-04-26 发票逾期预警相关请求入口
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("caution")

@Slf4j
public class ExtCautionController extends AbstractController {

    @Resource
    private IExtCautionExpirationService cautionExpirationService;

    /**
     * 逾期预警列表页
     *
     * @param reqVO
     *            查询条件
     * @return 返回
     */
    @PostMapping("list/listExpiration")
    @SysLog("逾期预警列表查询")
    public ResponseEntity<R> listExpiration(@RequestBody ExtCautionExpirationReqVO reqVO) {
        try {
            if (reqVO.getMonth() == null || reqVO.getMonth() < 1 || reqVO.getMonth() > 6) {
                return ResponseEntity.ok(R.error("请检查月份数值！"));
            }
            reqVO.setUserInfo(getUserInfo());
            if (StringUtils.isBlank(reqVO.getGfsh()) || "99".equals(reqVO.getGfsh())) {
                return ResponseEntity.ok(R.ok().put("data", "datalist:[]"));
            }
            return ResponseEntity.ok(Objects
                .requireNonNull(R.ok().put("data", cautionExpirationService.listExpiration(reqVO, getUserInfo()))));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 异常发票预警列表页
     *
     * @param reqVO
     *            查询条件
     * @return 返回
     */
    @PostMapping("list/listAnomalyInvoice")
    @SysLog("异常发票预警列表查询")
    public ResponseEntity<R> listAnomalyInvoice(@RequestBody ExtCautionAbnormalReqVO reqVO) {
        try {
            reqVO.setUserInfo(getUserInfo());
            if ("99".equals(reqVO.getGfsh()) || StringUtils.isBlank(reqVO.getGfsh())) {
                List<String> gfshAll = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
                log.debug("gfshAll : " + gfshAll);
                if (gfshAll.size() == 0) {
                    return ResponseEntity.ok(R.ok().put("data", "datalist:[]"));
                }
            }
            return ResponseEntity.ok(R.ok().put("data", cautionExpirationService.listAnomalyInvoice(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 普票跨年度预警列表页
     *
     * @param reqVO
     *            查询交件
     * @return 返回
     */
    @PostMapping("list/listMaximalInvoice")
    @SysLog("普票跨年度预警列表查询")
    public ResponseEntity<R> listMaximalInvoice(@RequestBody ExtCautionMaximalReqVO reqVO) {
        try {
            reqVO.setUserInfo(getUserInfo());
            if (StringUtils.isBlank(reqVO.getGfsh()) || "99".equals(reqVO.getGfsh())) {
                List<String> gfshAll = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
                if (gfshAll.size() == 0) {
                    return ResponseEntity.ok(R.ok().put("data", "datalist:[]"));
                }
            }
            return ResponseEntity.ok(R.ok().put("data", cautionExpirationService.listMaximalInvoice(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/list/notInAccount")
    @SysLog("未入账发票预警")
    public ResponseEntity<R> notInAccount(@RequestBody Map<String,Object> params){
        return ResponseEntity.ok(R.ok());
    }

}
