package com.dxhy.manage.controller;

import java.io.UnsupportedEncodingException;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.Objects;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
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.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.alibaba.fastjson.JSONObject;
import com.dxhy.common.utils.R;
import com.dxhy.manage.config.ManageConstants;
import com.dxhy.manage.entity.SysDeptEntity;
import com.dxhy.manage.model.ReturnModel;
import com.dxhy.manage.model.TaxManageModel;
import com.dxhy.manage.service.SysDeptService;
import com.dxhy.manage.service.TaxManageService;

import cn.hutool.json.JSONUtil;
import lombok.extern.slf4j.Slf4j;

/**
 * @author jiaohongyang
 */
@RestController
@RequestMapping("/taxno")
@Slf4j
public class TaxnoManageController {

    @Resource
    private SysDeptService sysDeptService;
    @Resource
    private TaxManageService taxManageService;

    @RequestMapping("/getTaxnoManage")
    public ResponseEntity<?> getTaxnoManage(@RequestBody Map<String, String> params) {
        String functionNo = params.get("functionNo");
        String optType = params.get("optType");
        String deptId = params.get("deptId");
        log.info("传入参数 类型：{} , 状态：{} , ID:{}", functionNo, optType, deptId);
        String manage = "";
        String optTypeManage = "";
        if (ManageConstants.FunctionNo.FUNCTION_NO_02.getCode().equals(functionNo)) {
            manage = ManageConstants.FunctionNo.FUNCTION_NO_02.getDesc();
        } else if (ManageConstants.FunctionNo.FUNCTION_NO_01.getCode().equals(functionNo)) {
            manage = ManageConstants.FunctionNo.FUNCTION_NO_01.getDesc();
        } else {
            manage = "FunctionNo，类型错误";
            return ResponseEntity.ok(R.error(manage));
        }
        if (ManageConstants.OptType.OPT_TYPE_0.getCode().equals(optType)) {
            optTypeManage = ManageConstants.OptType.OPT_TYPE_0.getDesc();
        } else if (ManageConstants.OptType.OPT_TYPE_1.getCode().equals(optType)) {
            optTypeManage = ManageConstants.OptType.OPT_TYPE_1.getDesc();
        } else if (ManageConstants.OptType.OPT_TYPE_2.getCode().equals(optType)) {
            optTypeManage = ManageConstants.OptType.OPT_TYPE_2.getDesc();
        } else {
            optTypeManage = "OptType，类型错误";
            return ResponseEntity.ok(R.error(optTypeManage));
        }

        SysDeptEntity sysDeptEntity = sysDeptService.queryDeptById(Long.valueOf(deptId));
        if (sysDeptEntity == null) {
            return ResponseEntity.ok(R.error("未查到相应税号信息"));
        }
        if (StringUtils.isEmpty(sysDeptEntity.getArea())) {
            return ResponseEntity.ok(R.error("地区编码不能为空"));
        }
        String code = sysDeptService.getTaxManage(sysDeptEntity, functionNo, optType);
        if (ManageConstants.ERROR_CODE.equals(code)) {
            String dec = manage + "：" + optTypeManage + " 失败";
            return ResponseEntity.ok(R.error(dec));
        } else {
            String dec = manage + "：" + optTypeManage + " 成功";
            return ResponseEntity.ok(Objects.requireNonNull(R.ok().put("data", dec)));
        }

    }

    @RequestMapping("/getTaxInfo")
    public ResponseEntity<?> getTaxInfo(@RequestParam("taxno") String taxno) {
        log.info("传入税号：{}", taxno);
        ReturnModel taxInfo = taxManageService.getTaxInfo(taxno);
        StringBuilder build = new StringBuilder();
        if (ManageConstants.SUCCESS_CODE.equals(taxInfo.getCode())) {
            return ResponseEntity
                .ok(Objects.requireNonNull(R.ok().put("data", JSONUtil.toJsonPrettyStr(taxInfo.getData()))));
        } else {
            return ResponseEntity.ok(R.error("获取采集状态失败"));
        }

    }

    @RequestMapping("/getHttpLog")
    public ResponseEntity<?> getHttpLog(@RequestParam("taxno") String taxno) {
        JSONObject obj = taxManageService.getTitle();
        List<TaxManageModel> list = taxManageService.getHttpLogs(taxno);
        obj.put("datalist", list);

        String jsonString = null;
        if (obj != null) {

            try {
                jsonString = Base64.getEncoder().encodeToString(obj.toJSONString().getBytes("UTF-8"));
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
            }
        }
        return ResponseEntity.ok(R.ok().put("data", jsonString));

    }

}
