package com.dxhy.base.controller;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

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.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.dxhy.base.service.tdxtaxcurrent.BaseITDxTaxCurrentService;
import com.dxhy.base.service.tdxtaxcurrent.BaseITDxTaxHandlelogService;
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.R;

import lombok.extern.slf4j.Slf4j;

/**
 *
 *
 * 企业税务信息入口
 *
 * @author 张宏
 * @date 2019年4月25日
 *
 */

@SuppressWarnings({"AlibabaUndefineMagicConstant", "AlibabaLowerCamelCaseVariableNaming",
    "AlibabaClassNamingShouldBeCamel"})
@RestController
@RequestMapping("qyxx")
@Slf4j
public class BaseTDxTaxCurrentController extends AbstractController {

    @Resource
    private BaseITDxTaxCurrentService baseITDxTaxCurrentService;

    @Resource
    private BaseITDxTaxHandlelogService baseITDxTaxHandlelogService;

    /**
     * 获取税号属期相关信息
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/qysqxx")
    @SysLog("获取税号属期相关信息")
    public ResponseEntity<?> getTaxCurrent(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        if (StringUtils.isNotBlank(gfsh) && !"99".equals(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseITDxTaxCurrentService.getTaxCurrent(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 企业税务信息查询
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/qyxxlb")
    @SysLog("调用企业税务信息查询接口")
    public ResponseEntity<?> listTaxCurrent(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String xydj = (String)map.get("xydj");
        String sbzq = (String)map.get("sbzq");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        int count = 0;

        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        pramsMap.put("limit", map.get("limit"));
        List<String> gfshList = new ArrayList<>();
        if (!"99".equals(gfsh) && StringUtils.isNotBlank(gfsh)) {
            gfshList.add(gfsh);
        } else {
            gfshList = UserInfoUtil.getGfshAll(getUserInfo().getOrg());
        }
        pramsMap.put("gfsh", gfshList);
        if (!StringHelper.isBlank(xydj)) {
            if (!"99".equals(xydj)) {
                pramsMap.put("xydj", xydj);
            }
        } else {
            return ResponseEntity.ok(R.error("信用等级不能为空！"));
        }
        if (!StringHelper.isBlank(sbzq)) {
            if (!"99".equals(sbzq)) {
                pramsMap.put("sbzq", sbzq);
            }
        } else {
            return ResponseEntity.ok(R.error("申报周期不能为空！"));
        }
        pramsMap.put("company", map.get("company"));
        pramsMap.put("menuId", map.get("menuId"));
        try {
            return ResponseEntity
                .ok(R.ok().put("data", baseITDxTaxCurrentService.selectByCxtj(pramsMap, curr, size, count)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 企业税务信息锁定或者释放
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/qyxxsd")
    @SysLog("调用企业税务信息锁定或者释放接口")
    public ResponseEntity<?> updateTaxCurrent(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String sdzt = (String)map.get("sdzt");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("userName", getUserInfo().getUsername());
        pramsMap.put("dbName", getUserInfo().getDbName());
        if (!StringHelper.isBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }
        if (!StringHelper.isBlank(sdzt)) {
            pramsMap.put("sdzt", sdzt);
        } else {
            return ResponseEntity.ok(R.error("锁定标志不能为空！"));
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseITDxTaxCurrentService.updateByGfsh(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 查询企业税务信息操作历史
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/czlslist")
    @SysLog("调用查询企业税务信息操作历史接口")
    public ResponseEntity<?> selectHandlelog(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        int curr = (int)map.get("curr");
        int size = (int)map.get("size");
        int count = 0;

        Map<String, Object> pramsMap = new HashMap<>(16);

        pramsMap.put("dbName", getUserInfo().getDbName());
        if (!StringHelper.isBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }
        try {
            return ResponseEntity
                .ok(R.ok().put("data", baseITDxTaxHandlelogService.selectHandlelogByGfsh(pramsMap, curr, size, count)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }

    /**
     * 设置抵扣限额
     *
     * @param map
     *            请求参数
     * @return 返回结果
     */
    @PostMapping("/szxe")
    @SysLog("调用企业税务信息设置抵扣限额接口")
    public ResponseEntity<?> updateLimit(@RequestBody Map<String, Object> map) {
        String gfsh = (String)map.get("gfsh");
        String dkse = (String)map.get("dkse");
        Map<String, Object> pramsMap = new HashMap<>(16);
        pramsMap.put("dbName", getUserInfo().getDbName());
        if (!StringHelper.isBlank(gfsh)) {
            pramsMap.put("gfsh", gfsh);
        } else {
            return ResponseEntity.ok(R.error("购方税号不能为空！"));
        }

        if (!StringHelper.isBlank(dkse) && new BigDecimal(dkse).intValue() < 0) {
            return ResponseEntity.ok(R.error("总税额不能为负数！"));
        } else {
            pramsMap.put("dkse", dkse);
        }
        try {
            return ResponseEntity.ok(R.ok().put("data", baseITDxTaxCurrentService.updateLimit(pramsMap)));
        } catch (Exception e) {
            e.printStackTrace();
            log.error("", e);
            return ResponseEntity.ok(R.error(CommonConstants.MSG_ERR_DEFAULT));
        }
    }
}
