package com.manage.service.ipml;

import com.manage.dao.Power_DeptMapper;
import com.manage.dao.Power_User_DictMapper;
import com.manage.entity.Power_Dept;
import com.manage.entity.Power_User;
import com.manage.service.Power_DeptService;
import com.manage.service.Power_Sys_DictService;
import com.manage.vo.Power_DeptVo;
import com.manage.vo.Power_UserVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.text.SimpleDateFormat;
import java.util.*;

/**
 * Author ly
 * Date 2019/4/15
 * Time 16:35
 * Description No Description
 */
@Service
public class Power_DeptServiceImpl implements Power_DeptService {

    @Autowired
    Power_DeptMapper powerDeptDao;

    @Autowired
    Power_Sys_DictService powerSysDictService;
    @Autowired
    Power_User_DictMapper powerUserDictMapper;

    @Override
    public int deleteByPrimaryKey(Integer dept_id) {
        return powerDeptDao.deleteByPrimaryKey(dept_id);
    }

    @Override
    public int insert(Power_Dept record) { return powerDeptDao.insert(record); }

    @Override
    public int insertSelective(Power_Dept powerDept) {
        //取得当前登录账号
        //取得当前登录账号
        ServletRequestAttributes attr=(ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request =attr.getRequest();
        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()为获取当前系统时间
        powerDept.setCreater(powerUser1.getUserName());
        powerDept.setCreateDate(date);
        powerDept.setUpdater(powerUser1.getUserName());
        powerDept.setUpdateDate(date);
        powerDept.setDeptCode("");
        return powerDeptDao.insertSelective(powerDept);
    }

    @Override
    public Power_Dept selectByPrimaryKey(Integer dept_id) {
        return powerDeptDao.selectByPrimaryKey(dept_id);
    }

    @Override
    public int updateByPrimaryKeySelective(Power_Dept powerDept,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()为获取当前系统时间
        powerDept.setUpdater(powerUser1.getUserName());
        powerDept.setUpdateDate(date);
        return powerDeptDao.updateByPrimaryKeySelective(powerDept); }

    @Override
    public int updateByPrimaryKey(Power_Dept record) {
        return powerDeptDao.updateByPrimaryKey(record);
    }

    @Override
    public List<Power_DeptVo> findSomeByMore(Power_Dept powerDept, HttpServletRequest request) {
        Power_UserVo powerUser1 = (Power_UserVo) request.getSession().getAttribute("CURRENT_USER");
        if(powerUser1.getRoleId() != 0){
            //查询归属医院
            powerDept.setDictId(powerUser1.getDictId());
        }
        List<Power_DeptVo> powerDeptList = powerDeptDao.findSomeByMore(powerDept);
        Set<String> perms = powerUser1.getMenus();
        if(null != powerDeptList && !powerDeptList.isEmpty()){
            for (Power_DeptVo powerDeptTemp : powerDeptList) {
                int isUpdate = 0;
                int isDelete = 0;
                for (String menu : perms) {
                    if ("/dept/update".equals(menu)) {
                        isUpdate = 1;
                    }
                    if ("/dept/delete".equals(menu)) {
                        isDelete = 1;
                    }
                }
                powerDeptTemp.setIsUpdate(isUpdate);
                powerDeptTemp.setIsDelete(isDelete);
            }
        }
        return powerDeptList;
    }

    @Override
    public int getTotal(Power_Dept powerDept) {
        return powerDeptDao.getTotal(powerDept);
    }

    @Override
    public List<Power_DeptVo> getDeptList() { return powerDeptDao.getDeptList(); }

    @Override
    public List<Power_Dept> checkDeptName(String deptName, Integer dictId) {
        return powerDeptDao.checkDeptName(deptName,dictId);
    }

    @Override
    public List<Power_Dept> selectByPrimaryKeys(String dept_ids) {
        String[] deptIdArr = dept_ids.split(",");
        Map<String,Object> map = new HashMap<>();
        map.put("list",deptIdArr);
        return powerDeptDao.selectByPrimaryKeys(map);
    }

    @Override
    public List<Power_Dept> selectDeptByDeptName(String deptNames) {
        String[] deptName = deptNames.split(",");
        Map<String,Object> map = new HashMap<>();
        map.put("list",deptName);
        return powerDeptDao.selectDeptByDeptName(map);
    }

    @Override
    public void export(Power_DeptVo powerDept,HttpServletRequest request,HttpServletResponse response) throws Exception {
        List<Power_DeptVo> depts = this.findSomeByMore(powerDept,request);
        if(null != depts && !depts.isEmpty()){
            for (Power_DeptVo dept : depts) {
                if (dept.getEffective() == 1) {
                    dept.setEffectiveCn("是");
                } else {
                    dept.setEffectiveCn("否");
                }
            }
        }
        String tableThNames = "科室名称,所属医院,是否有效,创建时间,创建人,修改时间,修改人";
        String fieldCns = "deptName,hospitalName,effectiveCn,createDate,creater,updateDate,updater";
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        //非系统管理员不显示所属医院信息
        if(user.getRoleId() != 0){
            tableThNames = tableThNames.replace("所属医院,","");
            fieldCns = fieldCns.replace("hospitalName,","");
        }
        //文件名
        String fileName = "科室列表(" + new SimpleDateFormat("yyyy_MM_dd HH_mm_ss").format(new Date()) + ").xls";
        //ExportExcelUtil
        com.manage.util.ExportExcelUtil exportExcelUtil = new com.manage.util.ExportExcelUtil();
        //导出excel的操作
        exportExcelUtil.expordExcel(tableThNames,fieldCns,depts,fileName,response);
    }

    @Override
    public void exportBlood(Power_DeptVo powerDept,HttpServletRequest request,HttpServletResponse response) throws Exception {
        List<Power_DeptVo> depts = this.findSomeByMore(powerDept,request);
        if(null != depts && !depts.isEmpty()){
            for (Power_DeptVo dept : depts) {
                if (dept.getEffective() == 1) {
                    dept.setEffectiveCn("是");
                } else {
                    dept.setEffectiveCn("否");
                }
            }
        }
        String tableThNames = "部门名称,所属医院,是否有效,创建时间,创建人,修改时间,修改人";
        String fieldCns = "deptName,hospitalName,effectiveCn,createDate,creater,updateDate,updater";
        Power_User user = (Power_User)request.getSession().getAttribute("CURRENT_USER");
        //非系统管理员不显示所属医院信息
        if(user.getRoleId() != 0){
            tableThNames = tableThNames.replace("所属医院,","");
            fieldCns = fieldCns.replace("hospitalName,","");
        }
        //文件名
        String fileName = "部门列表(" + new SimpleDateFormat("yyyy_MM_dd HH_mm_ss").format(new Date()) + ").xls";
        //ExportExcelUtil
        com.manage.util.ExportExcelUtil exportExcelUtil = new com.manage.util.ExportExcelUtil();
        //导出excel的操作
        exportExcelUtil.expordExcel(tableThNames,fieldCns,depts,fileName,response);
    }

    @Override
    public List<Power_DeptVo> selectDeptByUserId(HttpServletRequest request) {
        //取得当前登录账号
        Power_User user =(Power_User) request.getSession().getAttribute("CURRENT_USER");
        Integer userId = null;
        if(null != user.getRoleId() && user.getRoleId() != 0){
            userId = user.getUserId();
        }
        return powerDeptDao.selectDeptByUserId(userId);
    }

    @Override
    public List<Power_DeptVo> selectDeptByDictId(Integer dictId,String sysFlag) {
        return powerDeptDao.selectDeptByDictId(dictId,sysFlag);
    }
}
