package com.yanzhu.wechat.controller;

import com.yanzhu.base.domain.BaseAssetsType;
import com.yanzhu.base.service.IBaseAssetsTypeService;
import com.yanzhu.common.constant.Constants;
import com.yanzhu.common.core.controller.BaseController;
import com.yanzhu.common.core.domain.AjaxResult;
import com.yanzhu.common.core.domain.entity.SysDept;
import com.yanzhu.common.core.domain.entity.SysRole;
import com.yanzhu.common.core.domain.entity.SysUser;
import com.yanzhu.common.core.page.TableDataInfo;
import com.yanzhu.common.core.redis.RedisCache;
import com.yanzhu.common.enums.ShiFouEnum;
import com.yanzhu.common.utils.DictUtils;
import com.yanzhu.common.utils.SecurityUtils;
import com.yanzhu.common.utils.StringUtils;
import com.yanzhu.project.domain.ProProjectInfo;
import com.yanzhu.project.service.IProProjectApplyService;
import com.yanzhu.project.service.IProProjectInfoService;
import com.yanzhu.sur.domain.SurMenuConfig;
import com.yanzhu.sur.service.ISurMenuConfigService;
import com.yanzhu.system.service.ISysDeptService;
import com.yanzhu.system.service.ISysPostService;
import com.yanzhu.system.service.ISysRoleService;
import com.yanzhu.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
import java.util.concurrent.TimeUnit;
import java.util.stream.Collectors;

/**
 * 项目申请Controller
 * 
 * @author yanZhu
 * @date 2024-02-23
 */
@RestController
@RequestMapping("/wxApi/publics")
public class WxPublicsController extends BaseController
{

    @Autowired
    private RedisCache redisCache;

    @Autowired
    private ISysUserService userService;

    @Autowired
    private ISysDeptService deptService;

    @Autowired
    private ISurMenuConfigService surMenuConfigService;

    @Autowired
    private IBaseAssetsTypeService baseAssetsTypeService;

    @Autowired
    private IProProjectInfoService proProjectInfoService;

    @Autowired
    private IProProjectApplyService proProjectApplyService;

    /**
     * 获取项目申请详细信息
     */
    @GetMapping(value = "/projectApply/{id}")
    public AjaxResult projectApplyInfo(@PathVariable("id") Long id)
    {
        return success(proProjectApplyService.selectProProjectApplyById(id));
    }

    /**
     * 查询物资类型
     */
    @GetMapping("/v1/findAllByCategory/{category}")
    public AjaxResult findAllByCategory(@PathVariable String category)
    {
        SysUser sysUser = super.getLoginUser().getUser();
        String key = "YANZHU.BASE.ASSETSTYPE.findAllByCategory." + sysUser.getParDeptId() + "." + category;
        Object object = redisCache.getCacheObject(key);
        if (object != null) {
            //return success(object);
        }
        List<BaseAssetsType> list = baseAssetsTypeService.findAllByCategory(category);
        redisCache.setCacheObject(key, list, Constants.BASE_DATA_EXPIRATION, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 获取用户列表
     */
    @GetMapping("/user/list")
    public TableDataInfo userList(SysUser user)
    {
        startPage();
        List<SysUser> list = userService.selectUserList(user);
        return getDataTable(list);
    }

    /**
     * 根据用户编号获取详细信息
     */
    @GetMapping(value = "/user/info")
    public AjaxResult userInfo()
    {
        return success(super.getLoginUser().getUser());
    }

    /**
     * 获取部门树列表
     */
    @GetMapping("/dept/deptTree")
    public AjaxResult deptTree(SysDept dept)
    {
        return success(deptService.selectDeptTreeList(dept));
    }

    /**
     * 查询演训类型
     * @return
     */
    @GetMapping(value = "/dict/{key}")
    public AjaxResult dictValues(@PathVariable("key") String key){
        return success(DictUtils.getDictCache(key));
    }

    /**
     * 查询角色菜单列表
     */
    @GetMapping("/v1/selectRoleMenuList")
    public AjaxResult selectRoleMenuList(SurMenuConfig surMenuConfig)
    {
        SysUser sysUser = super.getLoginUser().getUser();
        if(!SysUser.isAdmin(sysUser.getUserId())){
            surMenuConfig.setUsername(sysUser.getPhonenumber());
        }
        String key = "YANZHU.SUR.V1.selectRoleMenuList." + sysUser.getUserId() ;
        Object object = redisCache.getCacheObject(key);
        if (object != null) {
            return success(object);
        }
        List<SurMenuConfig> list = surMenuConfigService.selectRoleMenuList(surMenuConfig);
        redisCache.setCacheObject(key, list, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 查询我的部门列表
     */
    @GetMapping("/v1/findMyDeptList")
    public AjaxResult findMyDeptList(){
        Long parDeptId = super.getLoginUser().getUser().getParDeptId();
        String key = "YANZHU.DEPT.V1.findMyDeptList." + parDeptId;
        Object object = redisCache.getCacheObject(key);
        if (object != null) {
            return success(object);
        }
        List<SysDept> list = deptService.findChildList(parDeptId);
        redisCache.setCacheObject(key, list, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        return success(list);
    }

    /**
     * 查询我的部门列表
     */
    @GetMapping("/v1/findMyProjectList")
    public AjaxResult findMyProjectList(){
        Long parDeptId = super.getLoginUser().getUser().getParDeptId();
        String key = "YANZHU.PROJ.V1.findMyProjectList." + parDeptId;
        Object object = redisCache.getCacheObject(key);
        if (object != null) {
            return success(object);
        }
        ProProjectInfo proProjectInfo = new ProProjectInfo();
        proProjectInfo.setDeptId(parDeptId);
        proProjectInfo.setIsDel(ShiFouEnum.FOU.getCode());
        List<ProProjectInfo> list = proProjectInfoService.selectProProjectInfoList(proProjectInfo);
        redisCache.setCacheObject(key, list, Constants.CAPTCHA_EXPIRATION, TimeUnit.MINUTES);
        return success(list);
    }

}
