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


import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.apache.ibatis.annotations.Param;
import org.aspectj.weaver.loadtime.Aj;
import org.jasig.cas.client.validation.Assertion;
import org.jeecg.common.constant.CommonConstant;
import org.jeecg.modules.cas.entity.ENT_USER;
import org.jeecg.modules.cas.entity.WellboreUser;
import org.jeecg.modules.cas.service.IWellboreAuthQuery;
import org.jeecg.modules.dataVue.entity.AFocusjh;
import org.jeecg.modules.dataVue.entity.Ajh;
import org.jeecg.modules.dataVue.mapper.AjhMapper;
import org.jeecg.modules.dataVue.mapper.MockMapper;
import org.jeecg.modules.dataVue.service.IAjhService;
import org.jeecg.modules.system.entity.SysDepart;
import org.jeecg.modules.system.entity.SysUser;
import org.jeecg.modules.system.service.ISysDepartService;
import org.jeecg.modules.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class AjhServiceImpl extends ServiceImpl<AjhMapper, Ajh>  implements IAjhService{
    @Resource
    private MockMapper mockMapper;

    @Autowired
    IWellboreAuthQuery iWellboreAuthQuery;


    @Resource
    private ISysUserService sysUserService;

    @Resource
    private ISysDepartService sysDepartService;

    @Override
    public List<Map<String, Object>> querySql(String sqlStr, String reserve) {
        return mockMapper.querySqlA(sqlStr);
    }

    @Override
    public List<Ajh> selectNotNullList() {
        return mockMapper.selectNotNullList();
    }

    @Override
    public int updateAjhWarnByJh(String jh ,  String warnTime , int count) {
        return mockMapper.updateAjhWarnByJh(jh,warnTime,count);
    }

    @Override
    public int querySqlCount(String sqlStr, String reserve) {
        return mockMapper.queryCountA(sqlStr);
    }

    @Override
    public String getRbEjdw(String ejdw){
//        switch (ejdw){
//            case "黄河钻井总公司":
//                return "黄河钻井";
//            case "渤海钻井总公司":
//                return "渤海钻井";
//            case "塔里木胜利钻井公司":
//                return "塔里木钻井";
//            case "黄河钻井总公司":
//                return "黄河钻井";
//            case "黄河钻井总公司":
//                return "黄河钻井";
//        }




        return ejdw;
    }

    @Override
    public Ajh selectAjhByJh(String jh){
        QueryWrapper<Ajh> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("JH", jh);
        return this.getOne(queryWrapper,false);
    }

    /**
     * 根据登录用户
     */
    @Override
    public void pageByUserDw(){
        //获取当前登录用户账户名
        String sessionCasName = iWellboreAuthQuery.getSessionCasName();
        //根据账户名获取当前登录用户单位信
        WellboreUser user = null;
        try {
            user=iWellboreAuthQuery.queryCasLogginedUserInformation(sessionCasName);
        } catch (IOException e) {
            e.printStackTrace();
        }
        if(!StringUtils.isEmpty(user)){
            ENT_USER ent_user = user.getENT_USER();
            log.info("统一认证平台登录用户信息:用户账户{};单位:{};另一个单位:{};所有单位:{}",ent_user.getEntUserName(),
                    ent_user.getOrgnaName(),ent_user.getPOrgnaName(),ent_user.getOrgMap().getOrgnaFullName());
            QueryWrapper<SysUser>  wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(SysUser::getUsername,sessionCasName).eq(SysUser::getRealname,ent_user.getEntUserName());
            SysUser userModel = sysUserService.getOne(wrapper);
            String orgCode = userModel.getOrgCode();
            //根据部门编码获取该部门及下级部门
            QueryWrapper<SysDepart> departQueryWrapper =new QueryWrapper<>();
            departQueryWrapper.lambda().eq(SysDepart::getId,orgCode).or().eq(SysDepart::getParentId,orgCode);
            List<SysDepart> departList = sysDepartService.list(departQueryWrapper);
            List<String> departNameAliasList = departList.stream().map(SysDepart::getDepartNameAlias).collect(Collectors.toList());
            log.info("部门:{}",departNameAliasList);
            QueryWrapper<Ajh> ajhQueryWrapper=new QueryWrapper<>();
            ajhQueryWrapper.lambda().in(Ajh::getEJDW,departNameAliasList);
            List<Ajh> list = this.list(ajhQueryWrapper);
            log.info("获取的井号:{}",list);
        }
    }

}
