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.ResponseBody;
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.paper.ExtPaperService;

import lombok.extern.slf4j.Slf4j;

/**
 * 票据采集-lxx
 *
 * @author dxhy
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("modules/paper")
@Slf4j
public class ExtPaperController extends AbstractController {

    @Resource
    private ExtPaperService extPaperService;

    /**
     * 票据采集
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/listInvoicePaper")
    @ResponseBody
    @SysLog("票据采集")
    public ResponseEntity<R> listBuyPaper(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        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());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);
        log.info("税号集合{},票据采集数据类型1-进项,2-销项{}", gfshList, pramsMap.get("dataType"));
        if (pramsMap.get("dataType") == null || "".equals(pramsMap.get("dataType"))) {
            pramsMap.put("dataType", "1");
        }
        if (pramsMap.get("cjksrq") != null && !"".equals(pramsMap.get("cjksrq"))) {
            pramsMap.put("cjksrq", DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("cjksrq").toString())));
        }

        if (pramsMap.get("cjjsrq") != null && !"".equals(pramsMap.get("cjjsrq"))) {
            pramsMap.put("cjjsrq", DateUtils.getNight(DateUtils.strToDate(pramsMap.get("cjjsrq").toString())));
        }
        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", extPaperService.listBuyInvoicePaper(pramsMap, curr, size)));

        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    /**
     * 票据查看
     *
     * @param pramsMap
     *            查询条件
     * @return 返回
     */
    @RequestMapping("/selectPaper")
    @ResponseBody
    @SysLog("票据查看")
    public ResponseEntity<R> selectPaper(@RequestBody Map<String, Object> pramsMap) {
        pramsMap.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (pramsMap.get("cjrq") != null && !"".equals(pramsMap.get("cjrq"))) {
            pramsMap.put("cjrq", pramsMap.get("cjrq").toString().substring(0, 10));
        }
        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());
            if (gfshList.size() == 0) {
                return ResponseEntity.ok(R.ok().put("data", ""));
            }
        }
        pramsMap.put("gfsh", gfshList);

        log.info("税号{},票据查看数据类型1-进项,2-销项{}", pramsMap.get("gfsh"), pramsMap.get("dataType"));
        if (pramsMap.get("dataType") == null || "".equals(pramsMap.get("dataType"))) {
            pramsMap.put("dataType", "1");
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", extPaperService.selectInvoicePaper(pramsMap)));

        } catch (Exception e) {
           log.info("业务处理异常：{}", e);
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

}
