package com.bonus.gs.sub.evaluate.evaluate.service;

import com.bonus.gs.sub.evaluate.evaluate.beans.TeamEvaluateBean;
import com.bonus.gs.sub.evaluate.evaluate.dao.TeamEvaluateDao;
import com.bonus.gs.sub.evaluate.manager.utils.AjaxRes;
import com.bonus.gs.sub.evaluate.manager.utils.GlobalConst;
import com.bonus.gs.sub.evaluate.manager.utils.KtySmsUtil;
import com.bonus.gs.sub.evaluate.manager.utils.UserUtil;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.session.Session;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import java.util.Arrays;
import java.util.List;


/**
 * @className:TeamEvaluateServiceImpl
 * @author:cwchen
 * @date:2024-11-19-14:28
 * @version:1.0
 * @description:班组评价
 */
@Service(value = "TeamEvaluateService")
public class TeamEvaluateServiceImpl implements TeamEvaluateService {

    @Autowired
    private TeamEvaluateDao dao;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxRes evaluateData(TeamEvaluateBean bean) {
        AjaxRes ar = new AjaxRes();
        try {
            if (bean.getOperType() == 1) {
                // 评价
                bean.setEvUserId(Integer.parseInt(String.valueOf(UserUtil.getLoginUser().getId())));
                bean.setEvUserName(UserUtil.getLoginUser().getNickname());
                bean.setManager(UserUtil.getLoginUser().getNickname());
            } else if (bean.getOperType() == 2) {
                // 审核
                bean.setCheckUserId(Integer.parseInt(String.valueOf(UserUtil.getLoginUser().getId())));
                bean.setCheckUserName(UserUtil.getLoginUser().getNickname());
            }
            dao.evaluateData(bean);
            ar.setSucceedMsg("操作成功");
        } catch (Exception e) {
            e.printStackTrace();
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            ar.setFailMsg("操作失败");
            return ar;
        }
        return ar;
    }

    @Override
    public AjaxRes getEvaluateData(TeamEvaluateBean bean) {
        AjaxRes ar = new AjaxRes();
        TeamEvaluateBean vo = null;
        try {
            vo = dao.getEvaluateData(bean);
            ar.setSucceed(vo);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return ar;
    }

    @Override
    public AjaxRes notification(TeamEvaluateBean bean) {
        AjaxRes ar = new AjaxRes();
        try {
            String phones = bean.getPhone();
            if(StringUtils.isBlank(bean.getPhone())){
                ar.setFailMsg("手机号不能为空");
            }
            String[] phoneArray;
            if (phones.contains(",")) {
                phoneArray = StringUtils.split(phones, ",");
            } else {
                phoneArray = new String[]{phones};
            }
            KtySmsUtil ktySmsUtil = new KtySmsUtil();
            String content = "【甘肃教育培训系统提醒您】您有一个已退场的班组未进行评价，请抓紧评价！";

            boolean allSentSuccessfully = true;
            for (String phone : phoneArray) {
                String msg = ktySmsUtil.setPhoneMsg(phone.trim(), content);
                if (!msg.contains("ok")) {
                    allSentSuccessfully = false;
                    break;
                }
            }
            if (allSentSuccessfully) {
                ar.setSucceedMsg("发送成功");
            } else {
                ar.setFailMsg("发送失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            ar.setSucceedMsg("发送失败");
        }
        return ar;
    }

    @Override
    public List<TeamEvaluateBean> getExportExcelData(TeamEvaluateBean bean) {
        List<TeamEvaluateBean> list = null;
        try {
            bean.setEvUserId(Integer.parseInt(String.valueOf(UserUtil.getLoginUser().getId())));
            list = dao.getTeamEvaluateList(bean);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public String getSendPhone(TeamEvaluateBean bean) {
        try {
            return dao.getSendPhone(bean);
        } catch (Exception e) {
            e.printStackTrace();
            return null;
        }
    }
    /**
     * 获取退场班组评价列表
     * @param o
     * @return
     */
    @Override
    public List<TeamEvaluateBean> getTeamEvaluateList(TeamEvaluateBean o) {
        o.setEvUserId(Integer.parseInt(String.valueOf(UserUtil.getLoginUser().getId())));
        if ("5".equals(UserUtil.getLoginUser().getRoleId()) || Arrays.asList(UserUtil.getLoginUser().getRoleId().split(",")).contains("5")) {
            o.setRoleId("5");
        } else if ("6".equals(UserUtil.getLoginUser().getRoleId()) || Arrays.asList(UserUtil.getLoginUser().getRoleId().split(",")).contains("6")) {
            o.setRoleId("6");
        }else if ("1".equals(UserUtil.getLoginUser().getRoleId()) || Arrays.asList(UserUtil.getLoginUser().getRoleId().split(",")).contains("1")) {
            o.setRoleId("1");
        }else{
            o.setRoleId("0");
        }
        return dao.getTeamEvaluateList(o);
    }
}
