package com.dxhy.base.controller;

import java.util.*;

import javax.annotation.Resource;

import com.dxhy.common.utils.DateUtils;
import org.apache.commons.lang.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.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.dxhy.base.service.fpcj.BaseIFpcjService;
import com.dxhy.base.service.fpcj.BaseIFpcjmxService;
import com.dxhy.common.aspect.SysLog;
import com.dxhy.common.constant.CommonConstants;
import com.dxhy.common.controller.AbstractController;
import com.dxhy.common.util.InvoiceUtil;
import com.dxhy.common.util.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.R;

import lombok.extern.slf4j.Slf4j;

/**
 *
 *
 * 发票采集接口
 *
 * @author ld
 * @date 2019年4月25日
 *
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaLowerCamelCaseVariableNaming"})
@RestController
@Slf4j
public class BaseFpcjController extends AbstractController {

    @Resource
    private BaseIFpcjService baseIFpcjService;

    @Resource
    private BaseIFpcjmxService baseIFpcjmxService;

    /**
     * 采集汇总列表接口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/cjhz/list")
    @ResponseBody
    @SysLog("调用采集汇总列表接口")
    public ResponseEntity<?> listCjhz(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String cjksrq = (String)map.get("cjksrq");
        String cjjsrq = (String)map.get("cjjsrq");
        Integer ycpbz = (Integer)map.get("ycpbz");
        Integer curr = (Integer)map.get("curr");
        Integer size = (Integer)map.get("size");

        map.put("userInfo", getUserInfo().getOrg());
        map.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (StringUtils.isNotBlank(gfsh) && !"99".equals(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        map.put("gfsh", gfshList);

        if (StringUtils.isNotBlank(cjksrq)) {
            map.put("cjksrq", cjksrq);
        } else {
            return ResponseEntity.ok(R.error("采集日期不能为空！"));
        }
        if (StringUtils.isNotBlank(cjjsrq)) {
            map.put("cjjsrq", cjjsrq);
        } else {
            return ResponseEntity.ok(R.error("采集日期不能为空！"));
        }
        if (1 == ycpbz) {
            map.put("ycpbz", ycpbz + "");
        }
        if (0 == ycpbz) {
            map.put("ycpbz","");
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", baseIFpcjService.selectByGfshAndCjrq(map, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 采集明细列表接口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/cjmx/list")
    @ResponseBody
    @SysLog("调用采集明细列表接口")
    public ResponseEntity<?> listCjmx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String cjrq = (String)map.get("cjrq");
        int ycpbz = (int)map.get("ycpbz");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());

        if (StringUtils.isNotBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方信息不能为空！"));
        }
        if (StringUtils.isNotBlank(cjrq)) {
            cjrq = DateUtils.getNight(DateUtils.strToDate(cjrq));
            pramsMap.put("cjrq", cjrq);
        } else {
            return ResponseEntity.ok(R.error("采集日期不能为空！"));
        }
        if (ycpbz == 1) {
            pramsMap.put("ycpbz", String.valueOf(ycpbz));
        }

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

    }

    /**
     * 未补发票明细列表接口
     *
     * @param map
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/wbfpxx/list")
    @ResponseBody
    @SysLog("调用未补发票明细列表接口")
    public ResponseEntity<?> listWbfpmx(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String cjksrq = (String)map.get("cjksrq");
        String cjjsrq = (String)map.get("cjjsrq");
        String invoiceNo = (String)map.get("invoiceNo");
        String invoiceType = (String)map.get("invoiceType");
        //发票来源
        String invoiceSource = (String)map.get("invoiceSource");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        if (StringUtils.isBlank(invoiceType)) {
            invoiceType = "0";
        }

        map.put("dbName", getUserInfo().getDbName());
        List<String> gfshList = new ArrayList<>();
        if (StringUtils.isNotBlank(gfsh) && !"99".equals(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        map.put("gfsh", gfshList);

//        if (!StringHelper.isBlank(invoiceNo)) {
//            InvoiceUtil iu = new InvoiceUtil();
//            iu.setFphm(invoiceNo);
//            if (iu.fphmValid()) {
//                map.put("invoiceNo", invoiceNo);
//            } else {
//                return ResponseEntity.ok(R.error("请输入8位或者10位的发票号码！"));
//            }
//        }
        if (!StringHelper.isBlank(invoiceNo)){
            if(invoiceNo.length()==8){
                map.put("invoiceNo", invoiceNo);
            }else {
                map.put("invoiceNo", "");
                map.put("eleInvoiceNo", invoiceNo);
            }
        }
            if (StringUtils.isNotBlank(invoiceType) && !"99".equals(invoiceType)) {
            map.put("invoiceType", invoiceType);
        } else {
            map.put("invoiceType", "");
        }
        map.put("invoiceSource",invoiceSource);

        if (StringUtils.isNotBlank(cjksrq)) {
            map.put("cjksrq", cjksrq);
        } else {
            return ResponseEntity.ok(R.error("采集日期不能为空！"));
        }
        if (StringUtils.isNotBlank(cjjsrq)) {
            map.put("cjjsrq", cjjsrq);
        } else {
            return ResponseEntity.ok(R.error("采集日期不能为空！"));
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", baseIFpcjmxService.selectWbfpmx(map, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 对未补录明细发票的明细进行补录
     *
     * @param postInvoiceDetail
     *            请求参数
     * @return 返回结果
     *
     */
    @PostMapping("/wbfpxx/insert")
    @ResponseBody
    @SysLog("手动查验对未补录明细发票的明细进行补录")
    public ResponseEntity<?> insertInvoiceDetail(@RequestBody String postInvoiceDetail) {

        try {
            JSONObject invoiceDetail = JSONObject.parseObject(postInvoiceDetail);
            if (!invoiceDetail.containsKey("invoiceType")
                || StringHelper.isBlank(invoiceDetail.getString("invoiceType"))) {
                return ResponseEntity.ok(R.error("发票类型不能为空！"));
            }
            if (!invoiceDetail.containsKey("invoiceDetail")) {
                return ResponseEntity.ok(R.error("发票明细不能为空！"));
            } else {
                JSONArray detailArray = (JSONArray)invoiceDetail.get("invoiceDetail");
                if (detailArray == null || detailArray.size() == 0) {
                    return ResponseEntity.ok(R.error("发票明细不能为空！"));
                }
            }

            baseIFpcjmxService.insertInvoiceDetail(invoiceDetail, getUserInfo().getDbName());
            return ResponseEntity.ok(R.ok().put("data", "查验成功"));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

}
