package com.tax.user.web.controller;

import com.google.common.collect.Lists;
import com.tax.user.model.SysClientUser;
import com.tax.user.service.api.SysApplyUserTaxnoService;
import com.tax.user.service.api.SysClientUserService;
import com.tax.user.service.safe.RestfulCheckManager;
import com.tax.user.service.safe.impl.RestFulCheckService;
import com.yxcheng.token.common.annotation.LogT;
import com.yxcheng.token.common.controller.BaseClientController;
import com.yxcheng.token.common.enumutil.ResultEnum;
import com.yxcheng.token.common.model.*;
import com.yxcheng.token.utils.ObjectUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
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.List;
import java.util.Map;

@RestController
@RequestMapping("/client")
public class UserController extends BaseClientController {
    @Autowired
    private SysApplyUserTaxnoService sysApplyUserTaxnoService;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private RestFulCheckService restFulCheckService;
    @Autowired
    private SysClientUserService sysClientUserService;


    @RequestMapping(value ="/register" , method = RequestMethod.POST)
    @LogT
    public JsonResult<String> register(@RequestBody UserReq userReq, HttpServletRequest request){

        if(!ObjectUtil.checkData(userReq.getTaxno(),userReq.getUserName(),userReq.getApplyFunction(),userReq.getTaxName())){
            return new JsonResult().errorResponse(ResultEnum.param_error);
        }

        return sysApplyUserTaxnoService.register(userReq,request);
    }


    @RequestMapping(value ="/isSafe", method = RequestMethod.POST)
    @LogT
    public CrRpcResult<String> isSafe(@RequestBody SafeModel model){
        CrRpcResult<String> rpcResult = new CrRpcResult<>();
        restFulCheckService.checkData(RestfulCheckManager.transfer(model),new RestfulApi(),rpcResult);
        return rpcResult;
    }

    @RequestMapping(value ="/checkRule", method = RequestMethod.POST)
    @LogT
    public JsonResult<String> checkRule(@RequestBody RuleModel ruleModel){
        JsonResult<String> jsonResult = new JsonResult<>();
        boolean functionFlag = sysClientUserService.checkUserFunction(ruleModel);
        if(!functionFlag) {
            jsonResult.errorResponse(ResultEnum.user_not_permission);
            return jsonResult;
        }
        sysClientUserService.checkUserRule(ruleModel, jsonResult);
        return jsonResult;
    }

    @RequestMapping(value ="/flushUserToCache")
    @LogT
    public JsonResult<Object> flushUserToCache(HttpServletRequest request){
        JsonResult<Object> rpcResult = new JsonResult<>();
        String userName = request.getParameter("userName");
        Map<String, Integer> flushNumber = sysClientUserService.flushClientUserList(userName);
        rpcResult.setData(flushNumber);
        return rpcResult;
    }

    @RequestMapping(value ="/queryUserList", method = RequestMethod.POST)
    @LogT
    public List<String> queryUserList(){
        List<String> userNameList = Lists.newArrayList();
        List<SysClientUser> userList = sysClientUserService.queryUserList();
        for(SysClientUser user:userList) {
            userNameList.add(user.getUserName());
        }
        return userNameList;
    }

}
