package com.sooka.system.service.impl;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import com.sooka.common.core.text.Convert;
import com.sooka.common.utils.DateUtils;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.TempPerUser;
import com.sooka.system.mapper.TempPerUserMapper;
import org.apache.catalina.security.SecurityUtil;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.sooka.system.mapper.TTempPermissionMapper;
import com.sooka.system.domain.TTempPermission;
import com.sooka.system.service.ITTempPermissionService;


/**
 * tempPerService业务层处理
 *
 * @author lei
 * @date 2022-08-09
 */
@Service
public class TTempPermissionServiceImpl implements ITTempPermissionService
{
    @Autowired
    private TTempPermissionMapper tTempPermissionMapper;

    @Autowired
    private TempPerUserMapper tempPerUserMapper;

    /**
     * 查询tempPer
     *
     * @param id tempPerID
     * @return tempPer
     */
    @Override
    public TTempPermission selectTTempPermissionById(Long id)
    {
        return tTempPermissionMapper.selectTTempPermissionById(id);
    }

    /**
     * 查询tempPer列表
     *
     * @param tTempPermission tempPer
     * @return tempPer
     */
    @Override
    public List<TTempPermission> selectTTempPermissionList(TTempPermission tTempPermission)
    {
        return tTempPermissionMapper.selectTTempPermissionList(tTempPermission);
    }

    /**
     * 新增tempPer
     *
     * @param tTempPermission tempPer
     * @return 结果
     */
    @Override
    public int insertTTempPermission(TTempPermission tTempPermission)
    {
        for(Long userId : tTempPermission.getUsers()){
            tTempPermission.setUserId(userId);
            tTempPermission.setCreateTime(DateUtils.getNowDate());
            tTempPermission.setCreateBy(ShiroUtils.getSysUser().getUserName());
            tTempPermission.setStartTime(transferString2Date(tTempPermission.getDuration().split(" - ")[0]));
            tTempPermission.setEndTime(transferString2Date(tTempPermission.getDuration().split(" - ")[1]));
            tTempPermissionMapper.insertTTempPermission(tTempPermission);
        }
        return 1;
    }

    public static Date transferString2Date(String s) {
        Date date = new Date();
        try {
            date = new SimpleDateFormat("yyyy-MM-dd").parse(s);
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return date;
    }

    /**
     * 修改tempPer
     *
     * @param tTempPermission tempPer
     * @return 结果
     */
    @Override
    public int updateTTempPermission(TTempPermission tTempPermission)
    {
        tTempPermission.setUpdateTime(DateUtils.getNowDate());
        tTempPermission.setUpdateBy(ShiroUtils.getSysUser().getUserName());
        return tTempPermissionMapper.updateTTempPermission(tTempPermission);
    }

    /**
     * 删除tempPer对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Override
    public int deleteTTempPermissionByIds(String ids)
    {
        return tTempPermissionMapper.deleteTTempPermissionByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除tempPer信息
     *
     * @param id tempPerID
     * @return 结果
     */
    @Override
    public int deleteTTempPermissionById(Long id)
    {
        return tTempPermissionMapper.deleteTTempPermissionById(id);
    }

    @Override
    public List<TempPerUser> listUser(){
        return tempPerUserMapper.listUser();
    }
}
