package com.securitycontrol.system.base.service.impl;

import com.securitycontrol.common.core.utils.StringUtils;
import com.securitycontrol.common.core.utils.aes.DateTimeHelper;
import com.securitycontrol.common.core.utils.uuid.IdUtils;
import com.securitycontrol.common.core.web.domain.AjaxResult;
import com.securitycontrol.common.security.utils.SecurityUtils;
import com.securitycontrol.entity.system.base.dto.TeamQuEvalDto;
import com.securitycontrol.entity.system.base.vo.TeamQuEvalVo;
import com.securitycontrol.system.base.mapper.ITeamQuEvalMapper;
import com.securitycontrol.system.base.service.ITeamQuEvalService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

/**
 * @author：cwchen
 * @date：2024-03-13-17:51
 * @version：1.0
 * @description：班组质量评价-业务逻辑层
 */
@Service(value = "ITeamQuEvalService")
@Slf4j
public class TeamQuEvalServiceImpl implements ITeamQuEvalService {

    @Resource(name = "ITeamQuEvalMapper")
    private ITeamQuEvalMapper mapper;

    @Override
    public List<TeamQuEvalVo> getTeamQuEvalLists(TeamQuEvalDto dto) {
        List<TeamQuEvalVo> list = new ArrayList<>();
        try {
            list = mapper.getTeamQuEvalLists(dto);
        } catch (Exception e) {
            log.error("获取班组质量评价列表",e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult addOrUpdateTeamEval(TeamQuEvalVo vo) {
        try {
            if (SecurityUtils.getLoginUser() != null && SecurityUtils.getLoginUser().getSysUser() != null) {
                vo.setEvaluator(SecurityUtils.getLoginUser().getSysUser().getUserName());
            }
            if (StringUtils.isEmpty(vo.getEvalId())) {
                String evalId = IdUtils.getUUId();
                vo.setEvalId(evalId);
                vo.setType(1);
                vo.setEvalTime(DateTimeHelper.getNowTime());
            } else {
                vo.setType(2);
            }
            mapper.addOrUpdateTeamEval(vo);
        } catch (Exception e) {
            log.error("新增/修改班组质量评价", e);
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }

    @Override
    public AjaxResult getTeamEvalById(TeamQuEvalDto dto) {
        TeamQuEvalVo vo = new TeamQuEvalVo();
        try {
            vo = mapper.getTeamEvalById(dto);
        } catch (Exception e) {
            log.error("班组质量评价详情",e);
        }
        return AjaxResult.success(vo);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult delTeamEvalById(TeamQuEvalDto dto) {
        try {
            mapper.delTeamEvalById(dto);
        } catch (Exception e) {
            log.error("删除班组质量评价", e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
        return AjaxResult.success();
    }
}
