package com.mvc.controller.dic;

import com.mvc.constant.BaseConstant;
import com.mvc.entity.TDynamicrouter;
import com.mvc.model.DicCustomizeMenuModel;
import com.mvc.model.FacUserModel;
import com.mvc.model.ResultGeekerModel;
import com.mvc.service.dic.DicCustomizeMenuService;
import com.mvc.service.dic.FacUserService;
import com.mvc.service.router.TDynamicrouterService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;

@Api(tags = "自定义菜单字典")
@RequestMapping("/DicCustomizeMenuAction")
@Controller
public class DicCustomizeMenuAction {

    @Resource
    private DicCustomizeMenuService dicCustomizeMenuService;

    @Resource
    private FacUserService facUserService;

    @Resource
    private TDynamicrouterService dynamicrouterService;

    @ApiOperation("查询所有数据列表")
    @RequestMapping(value = "/saveDicCustomizeMenu", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel saveDicCustomizeMenu(String userId, String path) {
        ResultGeekerModel resultModel;
        try {
            String msg = "";
            FacUserModel userModel = facUserService.findFacUserByUserId(userId);
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                resultModel = new ResultGeekerModel("用户不存在或账号异常");
                return resultModel;
            }

            path = path.replaceAll(TDynamicrouterService.CUSTOMIZ_ROUTER_PATH_SYMBOL,"");//去除路径中的自定义标识符
            TDynamicrouter dynamicrouter =  dynamicrouterService.findDynamicrouterMateByPath(path);
            if (dynamicrouter == null || !dynamicrouter.getUseFlag().toString().equals(BaseConstant.VALID_FLAG)) {
                resultModel = new ResultGeekerModel("菜单不存在或路由异常");
                return resultModel;
            }

            msg = dicCustomizeMenuService.checkDataLegitimacy(userModel.getId(), dynamicrouter.getId(), 0);
            if(StringUtils.isNotEmpty(msg)){
                resultModel = new ResultGeekerModel(msg);
            }else{
                DicCustomizeMenuModel res = dicCustomizeMenuService.saveDicCustomizeMenu(userModel, dynamicrouter);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }

    @ApiOperation(" 删除字典记录")
    @RequestMapping(value = "/deleteDicCustomizeMenu", method = RequestMethod.GET)
    @ResponseBody
    public ResultGeekerModel deleteDicCustomizeMenu(String userId, String path) {
        ResultGeekerModel resultModel;
        try {
            String msg = "";

            FacUserModel userModel = facUserService.findFacUserByUserId(userId);
            if (userModel == null || !userModel.getUseFlag().equals(BaseConstant.VALID_FLAG)) {
                resultModel = new ResultGeekerModel("用户不存在或账号异常");
                return resultModel;
            }

            path = path.replaceAll(TDynamicrouterService.CUSTOMIZ_ROUTER_PATH_SYMBOL,"");//去除路径中的自定义标识符
            TDynamicrouter dynamicrouter =  dynamicrouterService.findDynamicrouterMateByPath(path);
            if (dynamicrouter == null || !dynamicrouter.getUseFlag().toString().equals(BaseConstant.VALID_FLAG)) {
                resultModel = new ResultGeekerModel("菜单不存在或路由异常");
                return resultModel;
            }

            msg = dicCustomizeMenuService.checkDataLegitimacy(userModel.getId(), dynamicrouter.getId(), 1);
            if(StringUtils.isNotEmpty(msg)){
                resultModel = new ResultGeekerModel(msg);
            }else{
                int res = dicCustomizeMenuService.deleteDicCustomizeMenu(userModel, dynamicrouter);
                resultModel = new ResultGeekerModel(res);
            }
        } catch (Exception e) {
            e.printStackTrace();
            resultModel = new ResultGeekerModel(e.getMessage());
        }
        return resultModel;
    }
}
