package com.dxhy.base.controller;

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.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.base.model.BaseInvoiceEditCondVO;
import com.dxhy.base.model.BaseInvoiceReqVO;
import com.dxhy.base.model.BaseReceiptOutCancelEditReqVO;
import com.dxhy.base.model.BaseReceiptOutReqVO;
import com.dxhy.base.service.receipt.BaseIReceiptInvoiceService;
import com.dxhy.base.service.receipt.BaseIReceiptOutService;
import com.dxhy.base.service.receipt.BaseTDxOutReasonService;
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 lombok.extern.slf4j.Slf4j;

/**
 * @author peipei.li
 * @date 2019-04-23 进项税请求入口
 */
@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaCollectionInitShouldAssignCapacity",
    "AlibabaLowerCamelCaseVariableNaming"})
@RestController
@RequestMapping("receipt")
@Slf4j
public class BaseInvoiceOutController extends AbstractController {

    @Resource
    private BaseIReceiptInvoiceService baseIReceiptInvoiceService;

    @Resource
    private BaseIReceiptOutService baseIReceiptOutService;

    @Resource
    private BaseTDxOutReasonService baseTDxOutReasonService;

    /**
     * 进项税转出，列表页数据
     *
     * @param reqVO
     *            请求参数
     * @return 返回结果
     */
    @PostMapping(value = "list/listReceipt")
    @SysLog("调用进项税转出列表接口")
    public ResponseEntity<R> listReceipt(@RequestBody BaseInvoiceReqVO 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", baseIReceiptInvoiceService.listReceipt(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 已转出进项税，列表页数据
     *
     * @return 返回结果
     */
    @SysLog("调用已转出进项税列表接口")
    @PostMapping(value = "list/listOutReceipt")
    public ResponseEntity<R> listOutReceipt(@RequestBody BaseReceiptOutReqVO 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", baseIReceiptOutService.listOutReceipt(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 进项税转出保存
     *
     * @return 返回结果
     */
    @PostMapping(value = "edit/transferTax")
    @SysLog("调用进项税转出保存接口")
    public ResponseEntity<R> transferTax(@RequestBody BaseInvoiceEditCondVO condVO) {
        try {
            String idStr = condVO.getPrimaryKey();
            if (StringUtils.isBlank(idStr)) {
                return ResponseEntity.ok(R.error("请传入操作记录！"));
            }
            return ResponseEntity.ok(R.ok().put("data", baseIReceiptOutService.transferTax(condVO, getUserInfo())));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            if (!"累计转出金额不能大于发票金额".equals(e.getMessage()) && !"累计转出税额不能大于发票税额".equals(e.getMessage())) {
                return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
            } else {
                return ResponseEntity.ok(R.error(e.getMessage()));
            }
        }
    }

    /**
     * 进项税弹框显示 ，下拉框内容
     *
     * @return 返回结果
     */
    @SysLog("调用进项税弹框显示下拉框内容接口")
    @PostMapping(value = "edit/listCond")
    public ResponseEntity<R> listCond() {
        try {
            Map<String, String> map = new HashMap<>();
            map.put("company", getUserInfo().getCompany());
            map.put("dbName", getUserInfo().getDbName());
            return ResponseEntity.ok(R.ok().put("data", baseIReceiptOutService.listCond(map)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 取消已转出进项税列表
     *
     * @param reqVO
     *            请求参数
     * @return 返回结果
     */
    @SysLog("调用取消已转出进项税列表接口")
    @PostMapping(value = "list/listCancelReceipt")
    public ResponseEntity<R> listCancelReceipt(@RequestBody BaseReceiptOutReqVO 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", baseIReceiptOutService.listCancelReceipt(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 取消已转出进项税 请求
     *
     * @return 返回结果
     */
    @PostMapping(value = "edit/cancelOut")
    @SysLog("调用取消已转出进项税接口")
    public ResponseEntity<R> cancelOut(@RequestBody BaseReceiptOutCancelEditReqVO reqVO) {
        try {
            if (StringUtils.isBlank(reqVO.getPrimaryKey())) {
                return ResponseEntity.ok(R.error("请传入操作记录！"));
            }
            if (StringUtils.isBlank(reqVO.getPrimaryKey())) {
                return ResponseEntity.ok(R.error("请传入操作记录！"));
            }
            reqVO.setUserInfo(getUserInfo());
            return ResponseEntity.ok(R.ok(baseIReceiptOutService.cancelOut(reqVO)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    @SysLog("调用转出原因配置列表接口")
    @PostMapping(value = "list/outReasonList")
    public ResponseEntity<R> outReasonList(@RequestBody Map<String, String> map) {
        try {
            Map<String, String> pramsMap = new HashMap<>();
            pramsMap.put("company", getUserInfo().getCompany());
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("curr", map.get("curr"));
            pramsMap.put("size", map.get("size"));
            return ResponseEntity.ok(R.ok().put("data", baseTDxOutReasonService.listOutReason(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    @SysLog("调用转出原因配修改接口")
    @PostMapping(value = "updateReason")
    public ResponseEntity<R> updateReason(@RequestBody Map<String, String> map) {
        String msg = "修改成功！";
        try {
            String outReason = map.get("outReason");
            if (StringUtils.isBlank(outReason)) {
                msg = "转内销原因不能为空！";
                return ResponseEntity.ok(R.error(msg));
            }
            Map<String, String> pramsMap = new HashMap<>();
            pramsMap.put("company", getUserInfo().getCompany());
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("reasonCode", map.get("reasonCode"));
            pramsMap.put("id", map.get("id"));
            pramsMap.put("outReason", map.get("outReason"));
            int code = baseTDxOutReasonService.updateOutReason(pramsMap);
            if (code == 2) {
                msg = "该转出原因已被使用，禁止修改！";
                return ResponseEntity.ok(R.error(msg));
            } else if (code == 1) {
                msg = "转出原因已存在，修改失败！";
                return ResponseEntity.ok(R.error(msg));
            }
            return ResponseEntity.ok(R.ok().put("data", msg));
        } catch (Exception e) {
            e.printStackTrace();
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    @SysLog("调用转出原因删除接口")
    @PostMapping(value = "deleteReason")
    public ResponseEntity<R> deleteReason(@RequestBody Map<String, String> map) {
        String msg = "删除成功！";
        try {
            Map<String, String> pramsMap = new HashMap<>();
            pramsMap.put("company", getUserInfo().getCompany());
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("reasonCode", map.get("reasonCode"));
            pramsMap.put("id", map.get("id"));
            int code = baseTDxOutReasonService.deleteOutReason(pramsMap);
            if (code == 1) {
                msg = "该转出原因已被使用，禁止删除！";
                return ResponseEntity.ok(R.error(msg));
            }
            return ResponseEntity.ok(R.ok().put("data", msg));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("转出原因增加接口异常:{}", e.getMessage());
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }

    @SysLog("调用转出原因增加接口")
    @PostMapping(value = "addReason")
    public ResponseEntity<R> addReason(@RequestBody Map<String, String> map) {
        String msg = "保存成功！";
        try {
            String outReason = map.get("outReason");
            if (StringUtils.isBlank(outReason)) {
                msg = "转内销原因不能为空！";
                return ResponseEntity.ok(R.error(msg));
            }
            Map<String, String> pramsMap = new HashMap<>();
            pramsMap.put("company", getUserInfo().getCompany());
            pramsMap.put("dbName", getUserInfo().getDbName());
            pramsMap.put("outReason", map.get("outReason"));
            pramsMap.put("userName", getUserInfo().getUsername());
            int code = baseTDxOutReasonService.addOutReason(pramsMap);
            if (code == 1) {
                msg = "转出原因已存在，添加失败！";
                return ResponseEntity.ok(R.error(msg));
            }
            return ResponseEntity.ok(R.ok().put("data", msg));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("转出原因增加接口异常:{}", e.getMessage());
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }

    }
}
