package com.manage.service.ipml;

import com.manage.dao.Power_Sys_DictMapper;
import com.manage.entity.Power_Dept;
import com.manage.entity.Power_Sys_Dict;
import com.manage.entity.Power_User;
import com.manage.service.Power_DeptService;
import com.manage.service.Power_Sys_DictService;
import com.manage.vo.Power_Sys_DictVo;
import com.manage.vo.Power_UserVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

@Service
public class Power_Sys_DictServiceImpl implements Power_Sys_DictService {
    @Autowired
    Power_Sys_DictMapper powerSysDictMapper;

    @Autowired
    Power_DeptService powerDeptService;

    @Override
    public int deleteByPrimaryKey(Integer dictId) {
        return powerSysDictMapper.deleteByPrimaryKey(dictId);
    }

    @Override
    public int insert(Power_Sys_Dict record) {
        return powerSysDictMapper.insert(record);
    }

    @Override
    public int insertSelective(Power_Sys_Dict powerSysDict,HttpServletRequest request) {
        HttpSession session= request.getSession();
        Power_User powerUser1 =(Power_User) session.getAttribute("CURRENT_USER");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间
        //创建医院附带操作
        if(StringUtils.isNoneBlank(powerSysDict.getHospitalName())){
            powerSysDict.setSysType("医院");
            powerSysDict.setDictStatus(1);
            powerSysDict.setDictEdit(1);
        }
        powerSysDict.setUpdater(powerUser1.getUserName());
        powerSysDict.setUpdateDate(date);
        powerSysDict.setCreater(powerUser1.getUserName());
        powerSysDict.setCreateDate(date);
        return powerSysDictMapper.insertSelective(powerSysDict);
    }

    @Override
    public Power_Sys_DictVo selectByPrimaryKey(Integer dictId) {
        return powerSysDictMapper.selectByPrimaryKey(dictId);
    }

    @Override
    public int updateByPrimaryKeySelective(Power_Sys_Dict powerSysDict,HttpServletRequest request) {
        Power_User powerUser1 =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");//设置日期格式
        String date = df.format(new Date());// new Date()为获取当前系统时间
        powerSysDict.setUpdater(powerUser1.getUserName());
        powerSysDict.setUpdateDate(date);
        powerSysDict.setCreater(powerUser1.getUserName());
        powerSysDict.setCreateDate(date);
        return powerSysDictMapper.updateByPrimaryKeySelective(powerSysDict);
    }


    @Override
    public List<Power_Sys_Dict> selectList() {
        return powerSysDictMapper.selectList();
    }

    @Override
    public List<Power_Sys_Dict> selectHosList(HttpServletRequest request) {
        Power_User powerUser1 =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        Integer userId = null;
        if(powerUser1.getRoleId() != 0){
            userId = powerUser1.getUserId();
        }
        return powerSysDictMapper.selectHosList(userId);
    }

    @Override
    public List<Power_Sys_DictVo> selectSysType() {
        return powerSysDictMapper.selectSysType();
    }

    @Override
    public List<Power_Sys_DictVo> selectAll(String key) {
        return powerSysDictMapper.selectAll(key);
    }


    @Override
    public Power_Sys_DictVo checkHospitalName(String hospitalName) {
        return powerSysDictMapper.checkHospitalName(hospitalName);
    }

    @Override
    public Power_Sys_DictVo getHospitalByParentId(Integer parentId, String sysType) {
        return powerSysDictMapper.getHospitalByParentId(parentId,sysType);
    }

    @Override
    public List<Power_Sys_Dict> checkSysFlagOrSysNameByDeptId(String sysFlag, String sysName,String deptIds) {
        return powerSysDictMapper.checkSysFlagOrSysNameByDeptId(sysFlag, sysName, deptIds);
    }

    @Override
    public int simpleInsertDict(Power_Sys_Dict powerSysDict, String deptIds,HttpServletRequest request) {
        SimpleDateFormat fmt = new SimpleDateFormat("yyyy-MM-dd");
        Power_UserVo userVo = (Power_UserVo)request.getSession().getAttribute("CURRENT_USER");
        powerSysDict.setDictId(null);
        String date = fmt.format(new Date());
        powerSysDict.setUpdater(userVo.getUserName());
        powerSysDict.setUpdateDate(date);
        powerSysDict.setCreater(userVo.getUserName());
        powerSysDict.setCreateDate(date);
        List<Power_Sys_Dict> list = new ArrayList<>();
        //查询已存在的改系统的科室集合
        List<Power_Sys_Dict> deptBySysFlagList = powerSysDictMapper.selectDeptIdByParentIdAndSysFlag(powerSysDict.getParentId(), powerSysDict.getSysFlag(),deptIds);
        if(null != deptBySysFlagList && !deptBySysFlagList.isEmpty()){
            //去重
            String[] depts = deptIds.split(",");
            List<String> resultList = new ArrayList<>(depts.length);
            Collections.addAll(resultList,depts);
            List<String> deptList = new ArrayList<>();
            for (Power_Sys_Dict powerSysDictTemp : deptBySysFlagList) {
                Integer deptStr = powerSysDictTemp.getDeptId();
                if (null != deptStr) {
                    deptList.add(deptStr.toString());
                }
            }
            resultList.removeAll(deptList);
            if(!resultList.isEmpty()){
                for (String s : resultList) {
                    Power_Sys_Dict dict = new Power_Sys_Dict();
                    powerSysDict.setDeptId(Integer.valueOf(s));
                    BeanUtils.copyProperties(powerSysDict, dict);
                    list.add(dict);
                }
            }
        }else{
            String[] depts = deptIds.split(",");
            for (String dept : depts) {
                if (StringUtils.isNoneBlank(dept)) {
                    Power_Sys_Dict dict = new Power_Sys_Dict();
                    powerSysDict.setDeptId(Integer.valueOf(dept));
                    BeanUtils.copyProperties(powerSysDict, dict);
                    list.add(dict);
                }
            }
        }
        return powerSysDictMapper.simpleInsertDict(list);
    }
}
