package org.jeecg.modules.performance.service.impl;

import cn.com.ctop.common.module.entity.SysCompany;
import cn.com.ctop.common.module.service.ISysCompanyService;
import cn.com.ctop.common.module.service.ISysRoleExtService;
import cn.com.ctop.common.module.utils.CtopMediaTypeUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.apache.shiro.SecurityUtils;
import org.jeecg.common.system.vo.LoginUser;
import org.jeecg.modules.ctop.mapper.ProjectMemberMapper;
import org.jeecg.modules.performance.service.IPerformanceInfoService;
import org.jeecg.modules.performance.service.IPerformanceQuarterService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *  Created by JQ.bi on 2020.6.10
 */
@Service
public class PerformanceInfoInfoServiceImpl implements IPerformanceInfoService {

    @Autowired
    ISysCompanyService sysCompanyService;
    @Autowired
    ISysRoleExtService sysRoleService;
    @Autowired
    ISysUserService sysUserService;
    @Autowired
    ProjectMemberMapper projectMemberMapper;
    @Autowired
    IPerformanceQuarterService performanceQuarterService;

    @Override
    public Map<String, Object> getPerformanceInfo() {

        Map<String,Object> result= new HashMap<>();
        //获取公司名称
        result.put("company",this.getCompany());
        //部门
        result.put("depart",this.getDepart());
        //获取媒体类型
        result.put("mediaType", CtopMediaTypeUtil.getMediaTypeAll());
        //获取年份，季度
        result.put("data",performanceQuarterService.getYearQuarter());
        //获取职位
        result.put("post",sysRoleService.list());

        return result;
    }

    //根据权限查看绩效
    private List<SysCompany> getCompany(){
        List<SysCompany> result;
        LoginUser sysUser = (LoginUser) SecurityUtils.getSubject().getPrincipal();
        String userId = sysUser.getId();
        Map<String,Object> role=sysUserService.queryRoleCode(userId);
        if("管理员".equals(role.get("roleName"))){
            result=sysCompanyService.list();
        }else {
            String companyId=projectMemberMapper.getCompanyIdByUserId(userId);
            QueryWrapper<SysCompany> queryWrapper= new QueryWrapper<>();
            queryWrapper.eq("company_id",companyId);
            result= sysCompanyService.list(queryWrapper);
        }
        return result;
    }
    //TODO 部门暂时写死
    private List<String> getDepart(){
        List<String> depart=new ArrayList<>();
        depart.add("运营部");
        depart.add("设计部");
        depart.add("销售部");
        return depart;
    }
}
