package com.bonus.system.service.impl;

import cn.hutool.core.collection.ListUtil;
import com.bonus.common.core.domain.R;
import com.bonus.system.api.model.SbdUser;
import com.bonus.system.mapper.SbdUserMapper;
import com.bonus.system.service.ISbdUserService;
import io.netty.util.internal.StringUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * @author 黑子
 * 送变电 用户角色 业务接口实现层
 *
 */
@Service
@Slf4j
public class SbdUserServiceImpl implements ISbdUserService {

        @Autowired
        private SbdUserMapper mapper;

        /**
         * 查询 sbd用户信息
         * @param username
         * @return
         */
        @Override
        public SbdUser getUserInfo(String username) {
                //第一种 普通用户查询及角色
                SbdUser user=getXmbUser(username);
                if(user==null){
                   user=getDeptUser(username);
                }
                return  user;

        }



        /**
         * 查询项目部用户信息
         * @param username
         * @return
         */
        /**
         * 技术员编码
         */
        public static  final String JSY_POST="2ca0d32b0f3611efa1940242ac130004";
        private SbdUser getXmbUser(String username) {
                        SbdUser user=mapper.getDcUserInfo(username);
                        Long bnsDeptId=mapper.getBonusDeptId(user);
                        if(bnsDeptId!=null){
                            user.setBnsDeptId(bnsDeptId);
                        }
                        List<String> roleList=new ArrayList<>();
                        if(user!=null){
                                //技术总工 、负责人、项目经理角色查询
                                List<SbdUser> role=mapper.getUserRoleByPro(user);
                                if(role!=null && !role.isEmpty()){
                                        List<String> roleIds=mapper.getRoleIdByCode(role);
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                                roleList.addAll(roleIds);
                                        }
                                }
                                user.setPostId(JSY_POST);
                                //是不是技术员
                                int num=mapper.getJsyUserRole(user);
                                if(num>0){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("jsy");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                                roleList.addAll(roleIds);
                                        }
                                }
                                //查询 技术员、材料员
                                if("材料员".equals(user.getPostName())){
                                    List<String> roleIds=mapper.getRoleIdByCodeKey("cly");
                                    if(roleIds!=null && !roleIds.isEmpty()){
                                        roleList.addAll(roleIds);
                                    }
                                } else  if("科长".equals(user.getPostName())){
                                    if(user.getDeptName().contains("技术科")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("jskkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }

                                    }else  if(user.getDeptName().contains("施工管理")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("sgglkkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }
                                    }else  if(user.getDeptName().contains("物资")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("wzkkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }
                                    }


                                } else  if("副科长".equals(user.getPostName())){
                                    if(user.getDeptName().contains("技术科")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("jskfkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }

                                    }else  if(user.getDeptName().contains("施工管理")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("sgglkfkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }
                                    }else  if(user.getDeptName().contains("物资")){
                                        List<String> roleIds=mapper.getRoleIdByCodeKey("wzkfkz");
                                        if(roleIds!=null && !roleIds.isEmpty()){
                                            roleList.addAll(roleIds);
                                        }
                                    }
                                }
                                user.setList(roleList);
                                return  user;
                        }

                return  null;
        }
        /**
         * 查询分甘肃部门审核信息
         * @param username
         * @return
         */
        private SbdUser getDeptUser(String username) {

                  SbdUser user=mapper.getSbdUser(username);
                  List<String> roleList=new ArrayList<>();
                  if(user!=null){
                     SbdUser dept=mapper.getDeptById(user);
                      user.setUnitId(dept.getCompanyId());
                      Long bnsDeptId=mapper.getBonusDeptId(user);
                      if(bnsDeptId!=null){
                          user.setBnsDeptId(bnsDeptId);
                      }

                     List<String> roleName=mapper.getUserPost(user);
                          if(roleName!=null && !roleName.isEmpty()){
                                 for (String name:roleName){
                                         if("材料员".equals(name)){
                                                 List<String> roleIds=mapper.getRoleIdByCodeKey("cly");
                                                 if(roleIds!=null && !roleIds.isEmpty()){
                                                         roleList.addAll(roleIds);
                                                 }
                                         }else if("科长".equals(name)){
                                                        if(dept!=null){
                                                                        if(!StringUtil.isNullOrEmpty(dept.getName())){
                                                                                if(dept.getName().contains("技术科")){
                                                                                        List<String> roleIds=mapper.getRoleIdByCodeKey("jskkz");
                                                                                        if(roleIds!=null && !roleIds.isEmpty()){
                                                                                                roleList.addAll(roleIds);
                                                                                        }

                                                                                }else  if(dept.getName().contains("施工管理")){
                                                                                        List<String> roleIds=mapper.getRoleIdByCodeKey("sgglkkz");
                                                                                        if(roleIds!=null && !roleIds.isEmpty()){
                                                                                                roleList.addAll(roleIds);
                                                                                        }
                                                                                }else  if(dept.getName().contains("物资")){
                                                                                        List<String> roleIds=mapper.getRoleIdByCodeKey("wzkkz");
                                                                                        if(roleIds!=null && !roleIds.isEmpty()){
                                                                                                roleList.addAll(roleIds);
                                                                                        }
                                                                                }


                                                                        }



                                                        }

                                         }else if("副科长".equals(name)){
                                                 if(dept!=null){
                                                         if(!StringUtil.isNullOrEmpty(dept.getName())){
                                                                 if(dept.getName().contains("技术科")){
                                                                         List<String> roleIds=mapper.getRoleIdByCodeKey("jskfkz");
                                                                         if(roleIds!=null && !roleIds.isEmpty()){
                                                                                 roleList.addAll(roleIds);
                                                                         }

                                                                 }else  if(dept.getName().contains("施工管理")){
                                                                         List<String> roleIds=mapper.getRoleIdByCodeKey("sgglkfkz");
                                                                         if(roleIds!=null && !roleIds.isEmpty()){
                                                                                 roleList.addAll(roleIds);
                                                                         }
                                                                 }else  if(dept.getName().contains("物资")){
                                                                         List<String> roleIds=mapper.getRoleIdByCodeKey("wzkfkz");
                                                                         if(roleIds!=null && !roleIds.isEmpty()){
                                                                                 roleList.addAll(roleIds);
                                                                         }
                                                                 }


                                                         }

                                                 }

                                         }

                                 }


                          }
                          user.setList(roleList);
                          return  user;
                  }

                return  null;
        }

}
