package com.dxhy.portrait.business.controller;

import static com.dxhy.common.utils.BaseContextHandler.getUsername;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

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.StringHelper;
import com.dxhy.common.util.UserInfoUtil;
import com.dxhy.common.utils.DateUtils;
import com.dxhy.common.utils.R;
import com.dxhy.common.vo.Business;
import com.dxhy.common.vo.Tax;
import com.dxhy.portrait.business.service.MixedSignatureService;

import lombok.extern.slf4j.Slf4j;

/**
 * @author
 * @date 2019/05/10 08:59:44
 */
@SuppressWarnings("AlibabaUndefineMagicConstant")
@RestController
@RequestMapping("/mixedSignature")
@Slf4j
public class MixedSignatureController extends AbstractController {

    @Resource
    private MixedSignatureService mixedSignatureService;

    @RequestMapping("/qsxxlist")
    @ResponseBody
    @SysLog("签收查询")
    public ResponseEntity<?> listQsfp(@RequestBody Map<String, Object> pramsMap) {
        // 入参统一在入口处理
        Map<String, Object> data = new HashMap<>(16);

        data.put("userId", getUserId());
        data.put("userName", getUsername());
        data.put("dbName", getUserInfo().getDbName());
        data.put("gfsh", pramsMap.get("gfsh"));
        data.put("invoiceNo", pramsMap.get("invoiceNo"));
        data.put("businessCode", pramsMap.get("businessCode"));
        data.put("notes", pramsMap.get("notes"));
        data.put("xfsh", pramsMap.get("xfsh"));

        List<Business> business = getUserInfo().getBusiness();
        if (business.size() > 0) {
            data.put("isBusiness", true);
        }
        String businessCode = (String)pramsMap.get("businessCode");
        if (!StringHelper.isBlank(businessCode) && !"99".equals(businessCode)) {
            data.put("business", businessCode);
        }

        if (pramsMap.get("createDateBegin") != null && !"".equals(pramsMap.get("createDateBegin"))) {
            data.put("createDateBegin",
                DateUtils.getMorning(DateUtils.strToDate(pramsMap.get("createDateBegin").toString())));
        }
        if (pramsMap.get("createDateEnd") != null && !"".equals(pramsMap.get("createDateEnd"))) {
            data.put("createDateEnd",
                DateUtils.getNight(DateUtils.strToDate(pramsMap.get("createDateEnd").toString())));
        }

        if (pramsMap.get("invoiceType") != null && !"".equals(pramsMap.get("invoiceType"))
            && !"99".equals(pramsMap.get("invoiceType"))) {
            data.put("invoiceType", pramsMap.get("invoiceType"));
        }
        if (pramsMap.get("qsStatus") != null && !"".equals(pramsMap.get("qsStatus"))
            && !"99".equals(pramsMap.get("qsStatus"))) {
            data.put("qsStatus", pramsMap.get("qsStatus"));
        }
        if (pramsMap.get("qzStatus") != null && !"".equals(pramsMap.get("qzStatus"))
            && !"99".equals(pramsMap.get("qzStatus"))) {
            data.put("qzStatus", pramsMap.get("qzStatus"));
        }
        if (pramsMap.get("fileType") != null && !"".equals(pramsMap.get("fileType"))
            && !"99".equals(pramsMap.get("fileType"))) {
            data.put("fileType", pramsMap.get("fileType"));
        }
        if (pramsMap.get("xfName") != null && !"".equals(pramsMap.get("xfName"))) {
            data.put("xfName", pramsMap.get("xfName"));
        }
        if (pramsMap.get("ewm") != null && !"".equals(pramsMap.get("ewm"))) {
            data.put("ewm", pramsMap.get("ewm"));
        }

        int curr = (int)pramsMap.get("curr");
        int size = (int)pramsMap.get("size");
        try {
            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.selectQsfpList(data, curr, size)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @PostMapping("/smyqs/fpqs")
    @ResponseBody
    @SysLog("扫描仪签收")
    public ResponseEntity<?> mixedSign(@RequestBody Map<String, String> pramsMap) {
        // 入参统一在入口处理
        // Map<String,String> pramsMap = new HashMap<String,String>(32);
        pramsMap.put("userId", getUserId());
        pramsMap.put("userName", getUserName());
        pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("company", getUserInfo().getCompany());
        pramsMap.put("productType", getUserInfo().getProductType());

        List<Tax> list = getUserInfo().getOrg();
        if (list != null && list.size() > 0) {
            pramsMap.put("taxNo", list.get(0).getTaxno());
        }

        if (StringUtils.isNoneBlank(pramsMap.get("businessCode")) && !"99".equals(pramsMap.get("businessCode"))) {
            List<Business> businessList = getUserInfo().getBusiness();
            for (Business business : businessList) {
                if (business.getBusinessCode().equals(pramsMap.get("businessCode"))) {
                    pramsMap.put("businessName", business.getBusinessName());
                }
            }
        } else {
            pramsMap.put("businessCode", "");
        }
        if (StringHelper.isBlank(pramsMap.get("scanPch"))) {

            return ResponseEntity.ok(R.error("扫描识别批次号不能为空！"));
        } else {
            pramsMap.put("pch", pramsMap.get("scanPch"));
        }
        if (StringHelper.isBlank(pramsMap.get("gfTaxNo")) || "99".equals(pramsMap.get("gfTaxNo"))) {

            return ResponseEntity.ok(R.error("请选择购方税号！"));
        } else {
            pramsMap.put("gfTaxNo", pramsMap.get("gfTaxNo"));
        }
        List<Tax> taxList = getUserInfo().getOrg();
        for (Tax tax : taxList) {
            if (pramsMap.get("gfTaxNo").equals(tax.getTaxno())) {
                pramsMap.put("gfName", tax.getTaxname());
            }
        }

        try {
            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.mixedSign(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @RequestMapping(value = "modify", method = RequestMethod.POST)
    public ResponseEntity<R> updateReimbursement(@RequestBody Map<String, String> param) {

        param.put("userId", String.valueOf(getUserId()));
        param.put("userName", getUserName());
        param.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
        param.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
        param.put("dbName", getUserInfo().getDbName());
        param.put("company", getUserInfo().getCompany());
        param.put("productType", getUserInfo().getProductType());
        try {
            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.updateReimbursement(param)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error().put("data", "修改失败"));
        }

    }

    /**
     * 确认
     *
     * @param
     * @return
     */
    @RequestMapping(value = "confirm", method = RequestMethod.POST)
    public ResponseEntity<R> confirmReimbursement(@RequestBody Map<String, String> map) {
        try {
            String id = map.get("id");
            String dbName = getUserInfo().getDbName();
            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.confirm(id, dbName)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error().put("data", "确认失败"));
        }

    }

    @RequestMapping(value = "/dele", method = RequestMethod.POST)
    public ResponseEntity<R> deleReimbursement(@RequestBody HashMap<String, String> map) {
        try {
            String dbName = getUserInfo().getDbName();
            map.put("dbName", dbName);
            String id = map.get("id");
            if (org.apache.commons.lang.StringUtils.isBlank(id)) {
                return ResponseEntity.ok(R.error().put("data", "请选择需要删除项"));
            }
            String isHunsao = map.get("isHunsao");
            if (StringUtils.isBlank(isHunsao)) {
                return ResponseEntity.ok(R.error().put("data", "请输入签收来源"));
            }
            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.deleReimbursement(map)));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error().put("data", "删除失败"));
        }

    }

    /*    @PostMapping("/reSendOCR")
    @ResponseBody
    public ResponseEntity<?> reSendOcr(@RequestBody Map<String, String> map) {
        try {
            String ewm = map.get("ewm");
            String id = map.get("id");
            log.info("ewm" + ewm + ";id" + id);
            Map<String, String> pramsMap = new HashMap<>(32);
            pramsMap.put("userId", getUserId());
            pramsMap.put("userName", getUserName());
            pramsMap.put("gfshAll", StringHelper.listToString(UserInfoUtil.getGfshAll(getUserInfo().getOrg())));
            pramsMap.put("gfmcAll", StringHelper.listToString(UserInfoUtil.getGfmcAll(getUserInfo().getOrg())));
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("company", getUserInfo().getCompany());
            pramsMap.put("ewm", ewm);
            pramsMap.put("id", id);
            pramsMap.put("productType", getUserInfo().getProductType());

            return ResponseEntity.ok(R.ok().put("data", mixedSignatureService.reSendOcr(pramsMap)));

        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error().put("data", "处理失败"));
        }
    }*/
}
