package com.tax.sign.web.controller;


import com.tax.sign.model.SignModel;
import com.yxcheng.token.common.annotation.LogT;
import com.yxcheng.token.common.cache.RedisService;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.JsonResult;
import com.yxcheng.token.constants.RedisContants;
import com.yxcheng.token.utils.DateUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;

@RestController
@RequestMapping("/taxNo")
@ComponentScan(basePackages = {"com.tax.sign.service"})
public class TaxNoController {
    private static Logger logger = Logger.getLogger(TaxNoController.class);

    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RedisService redisService;

    @RequestMapping(value ="/addTaxnoToCache" , method = RequestMethod.GET)
    @LogT
    public JsonResult addTaxnoToCache() {
        JsonResult<SignModel>jsonResult = new JsonResult<SignModel>();
        String taxNo = request.getParameter("taxNo");
        String userName = request.getParameter("userName");
        try {
               setTaxnoToRedis(userName,taxNo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.errorResponse(ResultEnum.flush_error);
        }
        return jsonResult;
    }


    @RequestMapping(value ="/checkTaxnoFromCache" , method = RequestMethod.GET)
    @LogT
    public JsonResult checkTaxnoFromCache() {
        JsonResult<Boolean>jsonResult = new JsonResult<Boolean>();
        String taxNo = request.getParameter("taxNo");
        String userName = request.getParameter("userName");
        try {
            boolean b = checkTaxnoFromRedis(userName, taxNo);
            jsonResult.setData(b);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.errorResponse(ResultEnum.flush_error);
        }
        return jsonResult;
    }

    @RequestMapping(value ="/delTaxnoFromCache" , method = RequestMethod.GET)
    @LogT
    public JsonResult delTaxnoFromCache() {
        JsonResult<SignModel>jsonResult = new JsonResult<SignModel>();
        String taxNo = request.getParameter("taxNo");
        String userName = request.getParameter("userName");
        try {
            delTaxnoFromRedis(userName,taxNo);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.errorResponse(ResultEnum.flush_error);
        }
        return jsonResult;
    }


    @RequestMapping(value ="/getAllTaxno" , method = RequestMethod.GET)
    @LogT
    public JsonResult getAllTaxno() {
        JsonResult jsonResult = new JsonResult<>();
        String userName = request.getParameter("userName");
        try {
          //  Map<Object, Object> data = redisService.scan(RedisContants.SIGN_TAXNO_LIST + userName);
            Set<String> data = redisService.keys(RedisContants.SIGN_TAXNO_LIST + userName);

            Set resultData = new HashSet();
            if(data!=null && data.size()>0){
                for(String d : data){
                    resultData.add(d.replace(RedisContants.SIGN_TAXNO_LIST + userName +":",""));
                }
            }
            jsonResult.setData(resultData);
        } catch (Exception e) {
            e.printStackTrace();
            return jsonResult.errorResponse(ResultEnum.flush_error);
        }
        return jsonResult;
    }





    private void delTaxnoFromRedis(String userName, String taxNo) {
        redisService.del(RedisContants.SIGN_TAXNO_LIST + userName + ":" + taxNo);
    }


    public boolean checkTaxnoFromRedis(String userName, String taxno) {
        return redisService.exists(RedisContants.SIGN_TAXNO_LIST + userName + ":" + taxno);
    }

    public void setTaxnoToRedis(String userName, String taxno) {
        redisService.set(RedisContants.SIGN_TAXNO_LIST + userName + ":" + taxno, DateUtils.getDataString(DateUtils.datetimeFormat));
    }
}
