package com.bonus.material.upOrDown.equipment.service.impl;

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.upOrDown.equipment.domain.EquipmentEntity;
import com.bonus.material.upOrDown.equipment.mapper.UpOrDownEquipmentMapper;
import com.bonus.material.upOrDown.equipment.service.UpOrDownEquipmentService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

import static com.bonus.common.biz.constant.MaterialConstants.ADMIN_ID;
import static com.bonus.common.biz.constant.MaterialConstants.PROVINCE_COMPANY_DEPT_ID;

/**
 * @author 30791
 * @version 1.0
 * Create by 2025/11/16 14:13
 */
@Service
@Slf4j
public class UpOrDownEquipmentServiceImpl implements UpOrDownEquipmentService {

    @Resource
    private UpOrDownEquipmentMapper upOrDownEquipmentMapper;

    @Override
    public List<EquipmentEntity> list(EquipmentEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
        if(ADMIN_ID.equals(userId)){
            entity.setCompanyId(1L);
        }else{
            entity.setCompanyId(deptId);
        }

        return upOrDownEquipmentMapper.list(entity);
    }

    @Override
    public List<EquipmentEntity> listByDown(EquipmentEntity entity) {
        Long deptId = SecurityUtils.getLoginUser().getSysUser().getDeptId();
        Long userId = SecurityUtils.getLoginUser().getUserid();
        // 管理员和省公司可查看所有数据
        if (userId != null && deptId != null
                && !userId.equals(ADMIN_ID)
                && !deptId.equals(PROVINCE_COMPANY_DEPT_ID)) {
            entity.setCompanyId(deptId);
        }

        return upOrDownEquipmentMapper.listByDown(entity);
    }

    @Override
    public void downEquipmentById(Long equipmentId) {
        upOrDownEquipmentMapper.downEquipmentById(equipmentId);

    }

    /**
     * 批量修改装备上架状态及租赁价
     * @param entity
     * @return
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult upEquipmentBatch(List<EquipmentEntity> entity) {
        try {
            entity.stream()
                    .filter(Objects::nonNull)
                    // 仅处理未修改价格的设备
                    .filter(equipment -> !equipment.getIsChangePrice())
                    .forEach(equipment -> {
                        log.debug("设备ID:{} 未修改价格，置空leasePrice", equipment.getMaId());
                        equipment.setLeasePrice(null);
                    });
            int result = upOrDownEquipmentMapper.upEquipmentBatch(entity);
            if (result == 0) {
                throw new RuntimeException("批量上架失败");
            }
        } catch (RuntimeException e) {
            log.error("批量上架失败:{}", e.getMessage());
        }
        return AjaxResult.success("批量上架成功");
    }

    @Override
    public void downEquipmentBatch(List<Long> ids) {
        upOrDownEquipmentMapper.downEquipmentBatch(ids) ;
    }


}
