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

import com.bonus.common.core.web.domain.AjaxResult;
import com.bonus.common.security.utils.SecurityUtils;
import com.bonus.material.upOrDown.tool.entity.ToolEntity;
import com.bonus.material.upOrDown.tool.mapper.UpOrDownToolMapper;
import com.bonus.material.upOrDown.tool.service.UpOrDownToolService;
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/15 23:11
 */

@Service
@Slf4j
public class UpOrDownToolServiceImpl implements UpOrDownToolService {

    @Resource
    private UpOrDownToolMapper upOrDownToolMapper;





    @Override
    public List<ToolEntity> list(ToolEntity 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);
//        }
        if(ADMIN_ID.equals(userId)){
            entity.setCompanyId(1L);
        }else{
            entity.setCompanyId(deptId);
        }
        return upOrDownToolMapper.list(entity);
    }

    @Override
    public List<ToolEntity> listByNumber(ToolEntity 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 upOrDownToolMapper.listByNumber(entity);
    }

    @Override
    public AjaxResult upToolByNumber(ToolEntity entity) {
        return null;
    }

    @Override
    public List<ToolEntity> listByCode(ToolEntity 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 upOrDownToolMapper.listByCode(entity);
    }

    @Override
    public AjaxResult upToolByCode(ToolEntity entity) {
        return null;
    }


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

    @Override
    public void downToolBatch(List<Long> ids) {
        upOrDownToolMapper.downToolBatch(ids) ;
    }

    @Override
    public void downToolById(Long toolId) {
        upOrDownToolMapper.downToolById(toolId);
    }



}
