package com.bonus.system.evection.service;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.system.api.domain.MapVo;
import com.bonus.system.api.domain.SysRole;
import com.bonus.system.evection.dao.EvectionDao;
import com.bonus.system.evection.entity.EvectionBean;
import com.bonus.system.holiday.dao.WorkReportDao;
import com.bonus.system.leaveReporting.entity.LeaveReportingBean;
import lombok.extern.log4j.Log4j2;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.*;

/**
 * 出差报备-业务层
 *
 * @author lsun
 */
@Service("EvectionService")
@Log4j2
public class EvectionServiceImpl implements EvectionService {

    @Resource(name = "EvectionDao")
    private EvectionDao evectionDao;



    @Override
    public List<EvectionBean> getEvectionList(EvectionBean bean) {
        Map<String, Object> params = bean.getParams();
        return evectionDao.getEvectionList(bean,params);
    }

    @Override
    public int insertEvection(EvectionBean o) {
        Long userId = SecurityUtils.getLoginUser().getSysUser().getUserId();

        // String cadre = SecurityUtils.getLoginUser().getSysUser().getIsCadre();
        // o.setIsCadre(cadre);
        String uuId = UUID.randomUUID().toString();
        o.setUuid(uuId);
        o.setCreateUserId(userId);
        o.setSource("1");
        String travelers = o.getTravelers();    
        String[] split = travelers.split(",");
        String[] splitName = o.getTravelersName().split(",");
        int k = 0;
        try {
            //先新增一次出差的
            evectionDao.insertEvection(o);
            //在循环加入出行人的
            for (int i = 0; i < split.length; i++) {
                String userIdStr = split[i];
                if(!userIdStr.trim().isEmpty()){
                    o.setUserId(Long.valueOf(split[i]));
                    o.setUserName(splitName[i]);
                    evectionDao.insertEvection(o);
                }
            }
            k = 1;
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return k;
    }

    @Override
    public int updateEvection(EvectionBean o) {
        //先删除之前的出行人
        int i = evectionDao.deleteEvectionById(o.getUuid());
        int k = 0;
        if(i>0){
            o.setUpdateUserId(SecurityUtils.getLoginUser().getSysUser().getUserId());
            o.setCreateUserId(SecurityUtils.getLoginUser().getSysUser().getUserId());
            o.setSource("1");
            String travelers = o.getTravelers();
            String[] split = travelers.split(",");

            String[] splitName = o.getTravelersName().split(",");
            try {
                //先新增一次出差的
                evectionDao.insertEvection(o);
                //在循环加入出行人的
                for (int j = 0; j < split.length; j++) {
                    String userIdStr = split[j];
                    if(!userIdStr.trim().isEmpty()){
                        o.setUserId(Long.valueOf(split[j]));
                        o.setUserName(splitName[j]);
                        evectionDao.insertEvection(o);
                    }
                }
                k = 1;
            }catch (Exception e) {
                log.error(e.toString(),e);
            }
        }
        return k;
    }

    @Override
    public int deleteEvectionById(String id) {
        return evectionDao.deleteEvectionById(id);
    }

    @Override
    public EvectionBean selectEvectionById(String id) {
        return evectionDao.selectEvectionById(id);
    }

    @Override
    public EvectionBean getPostName(Long id) {
        return evectionDao.getPostName(id);
    }

    @Override
    public AjaxResult getUserList(Long id) {
        try {
            EvectionBean bean = new EvectionBean();
            //只能查自己部门的人
            List<MapVo> orgList = SecurityUtils.getLoginUser().getSysUser().getOrgList();
            if(!orgList.isEmpty()){
                ArrayList<String> ids = new ArrayList<>();
                for (MapVo mapVo : orgList) {
                    ids.add(String.valueOf(mapVo.getId()));
                }
                bean.setOrgList(ids);
            }
            bean.setId(id);
            List<EvectionBean> list = evectionDao.getUserList(bean);
            return AjaxResult.success(list);
        } catch (Exception e) {
            log.error(e.toString(),e);
            return AjaxResult.error();
        }
    }

    @Override
    public Integer isCheck(String id,int type) {
        try {
            EvectionBean evectionBean = evectionDao.getEvectionById(id,type);
            List<Integer> list = evectionDao.isCheck(evectionBean);
            if(CollectionUtils.isNotEmpty(list)){
                for (Integer status : list) {
                    if(!Objects.equals(status,0)){
                        return status;
                    }
                }
            }
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return 0;
    }
}
