package com.bonus.app.service.impl;

import com.bonus.app.mapper.AppEquipmentReqMapper;
import com.bonus.app.mapper.AppSafetyHatMapper;
import com.bonus.app.service.IAppSafetyHatService;
import com.bonus.common.core.utils.encryption.Sm4Utils;
import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.entity.app.AppParamsDto;
import com.bonus.common.entity.app.vo.SafetyHatDataVo;
import com.bonus.common.entity.app.vo.SafetyHatVo;
import com.bonus.common.security.utils.SecurityUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
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;

/**
 * @className:AppSafetyHatServiceImpl
 * @author:cwchen
 * @date:2024-08-07-15:38
 * @version:1.0
 * @description:APP-智能安全帽
 */
@Service(value = "IAppSafetyHatService")
@Slf4j
public class AppSafetyHatServiceImpl implements IAppSafetyHatService {

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

    @Resource(name = "AppEquipmentReqMapper")
    private AppEquipmentReqMapper appEquipmentReqMapper;

    @Override
    public List<SafetyHatVo> getSafetyHatLists(AppParamsDto dto) {
        List<SafetyHatVo> list = new ArrayList<>();
        try {
            list = mapper.getSafetyHatLists(dto);
            for (SafetyHatVo vo : list) {
                if(StringUtils.isNotBlank(vo.getPhone())){
                    vo.setPhone(Sm4Utils.decode(vo.getPhone()));
                }
            }
        } catch (Exception e) {
            log.error(e.toString(),e);
        }
        return list;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult distributionHelmet(SafetyHatDataVo vo) {
        try {
            boolean isTeamRoleCode = SecurityUtils.isTeamRoleCode();
            if(!isTeamRoleCode){
                return AjaxResult.error("非班组角色，无分配安全帽设备权限");
            }
            // 班组分配安全帽设备
            mapper.distributionHelmet(vo);
            // 更新设备表领用状态
            mapper.updateDevLyStatus(vo);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error(e.toString(),e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }

    @Override
    public AjaxResult returnHelmet(AppParamsDto dto) {
        try {
            boolean isTeamRoleCode = SecurityUtils.isTeamRoleCode();
            if(!isTeamRoleCode){
                return AjaxResult.error("非班组角色，无归还安全帽设备权限");
            }
            // 更新人员领用的归还状态
            mapper.returnHelmet(dto);
            // 更新设备表解绑状态
            appEquipmentReqMapper.updateReturnDeviceData(dto,2);
            return AjaxResult.success();
        } catch (Exception e) {
            log.error(e.toString(),e);
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
            return AjaxResult.error();
        }
    }
}
