package com.tuoheng.admin.service.accident;

import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.tuoheng.admin.entity.Accident;
import com.tuoheng.admin.entity.Dept;
import com.tuoheng.admin.entity.Section;
import com.tuoheng.admin.entity.User;
import com.tuoheng.admin.enums.AccidentEnum;
import com.tuoheng.admin.enums.MarkEnum;
import com.tuoheng.admin.enums.DataPermissionEnum;
import com.tuoheng.admin.mapper.AccidentMapper;
import com.tuoheng.admin.mapper.DeptMapper;
import com.tuoheng.admin.mapper.SectionMapper;
import com.tuoheng.admin.mapper.UserMapper;
import com.tuoheng.admin.vo.accident.AccidentTipsVo;
import com.tuoheng.common.core.exception.ServiceException;
import com.tuoheng.common.core.utils.JsonResult;
import com.tuoheng.common.core.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author ChengWang
 * @Date 2023/3/14
 */
@Slf4j
@Service
public class AccidentTipsService {

    @Autowired
    private AccidentMapper accidentMapper;

    @Autowired
    private SectionMapper sectionMapper;

    @Autowired
    private DeptMapper deptMapper;

    @Autowired
    private UserMapper userMapper;

    /**
     * 事故告警提示
     *
     * @param accidentId
     * @return
     */
    public JsonResult getTips(String accidentId) {
        //校验
        if (StringUtils.isEmpty(accidentId)) {
            return JsonResult.error(AccidentEnum.ACCIDENT_ID_IS_NULL.getCode(), AccidentEnum.ACCIDENT_ID_IS_NULL.getMsg());
        }
        //根据事故id查询相应属性值
        Accident accident = accidentMapper.selectById(accidentId);
        if (ObjectUtils.isEmpty(accident)) {
            return JsonResult.error(AccidentEnum.DATA_IS_NULL.getCode(), AccidentEnum.DATA_IS_NULL.getMsg());
        }
        AccidentTipsVo accidentTipsVo = new AccidentTipsVo();
        BeanUtils.copyProperties(accident, accidentTipsVo);
        //路段范围
        if (StringUtils.isNotEmpty(accident.getSectionId())) {
            Section section = sectionMapper.selectById(accident.getSectionId());
            if (StringUtils.isNotEmpty(section.getSectionRange())) {
                accidentTipsVo.setSectionRange(section.getSectionRange());
            }
        }
        //监管部门  部门管理员
        if (StringUtils.isEmpty(accident.getDeptId())) {
            throw new ServiceException("事故对应的监管部门id为空");
        }
        Dept dept = deptMapper.selectById(accident.getDeptId());
        if (StringUtils.isNotEmpty(dept.getName())) {
            accidentTipsVo.setDeptName(dept.getName());
        }
        User user = userMapper.selectOne(Wrappers.<User>lambdaQuery()
                .eq(User::getDeptId, accident.getDeptId())
                .eq(User::getRoleId, DataPermissionEnum.DEPT_AND_SUB_DEPT.getCode())
                .eq(User::getMark, MarkEnum.VALID.getCode()));
        if(ObjectUtils.isEmpty(user)){
            throw new ServiceException("登录用户对应的部门没有管理员账户");
        }
        if(StringUtils.isNotEmpty(user.getRealname())){
            accidentTipsVo.setRealName(user.getRealname());
        }

        return JsonResult.success(accidentTipsVo);
    }
}
