package net.lab1024.sa.admin.module.business.base.area.service;

import java.util.*;

import net.lab1024.sa.admin.module.business.base.area.dao.AreaDao;
import net.lab1024.sa.admin.module.business.base.area.domain.entity.AreaEntity;
import net.lab1024.sa.admin.module.business.base.area.domain.form.AreaAddForm;
import net.lab1024.sa.admin.module.business.base.area.domain.form.AreaUpdateForm;
import net.lab1024.sa.admin.module.business.base.area.manager.AreaManager;
import net.lab1024.sa.admin.module.business.base.location.service.LocationQueryService;
import net.lab1024.sa.admin.util.JoinerResult;
import net.lab1024.sa.admin.util.ResponseDTOUtils;
import net.lab1024.sa.base.common.code.UserErrorCode;
import net.lab1024.sa.base.common.util.SmartBeanUtil;
import net.lab1024.sa.base.common.domain.ResponseDTO;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.stereotype.Service;
import jakarta.annotation.Resource;
import org.springframework.transaction.annotation.Transactional;

/**
 * 库区信息 Service
 *
 * @author hj
 * @since 2025-03-11 11:12:36
 * copyright 友仓
 */

@Service
public class AreaService {

    @Resource
    private AreaDao areaDao;

    @Resource
    private AreaManager areaManager;

    @Resource
    private AreaQueryService areaQueryService;

    @Resource
    private LocationQueryService locationQueryService;


    /**
     * 添加
     *
     * @param addForm 入参
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> add(AreaAddForm addForm) {
        AreaEntity existingArea = areaQueryService.queryByAreaName(addForm.getAreaName());
        if (existingArea != null) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, UserErrorCode.ALREADY_EXIST.getMsg());
        }
        AreaEntity areaEntity = SmartBeanUtil.copy(addForm, AreaEntity.class);
        areaDao.insert(areaEntity);
        //更新缓存
        areaManager.removeCache();
        return ResponseDTO.ok();
    }

    /**
     * 更新
     *
     * @param updateForm 入参
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> update(AreaUpdateForm updateForm) {
        AreaEntity existingArea = areaQueryService.queryByAreaName(updateForm.getAreaName());
        if (existingArea != null && !existingArea.getAreaId().equals(updateForm.getAreaId())) {
            return ResponseDTO.error(UserErrorCode.ALREADY_EXIST, UserErrorCode.ALREADY_EXIST.getMsg());
        }
        AreaEntity areaEntity = SmartBeanUtil.copy(updateForm, AreaEntity.class);
        areaDao.updateById(areaEntity);
        //更新缓存
        areaManager.removeCache();
        return ResponseDTO.ok();
    }

    /**
     * 批量删除
     *
     * @param idList 入参
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> batchDelete(List<Long> idList) {
        if (CollectionUtils.isEmpty(idList)) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }

        JoinerResult joiner = JoinerResult.createJoiner();
        List<Long> idsToDelete = new ArrayList<>();
        for (Long id : idList) {
            AreaEntity area = areaManager.queryArea(id);
            if (!locationQueryService.checkLocationExist(id)) {
                joiner.getErrorMsg().add(area.getAreaName() + "已关联库位,无法删除");
            } else {
                idsToDelete.add(id);
                joiner.getSussMsg().add(area.getAreaName() + "删除成功");
            }
        }

        //删除库区
        if (CollectionUtils.isNotEmpty(idsToDelete)) {
            areaManager.removeBatchByIds(idsToDelete);
            //更新缓存
            areaManager.removeCache();
        }

        return ResponseDTOUtils.buildResponseDTO(joiner);
    }

    /**
     * 单个删除
     *
     * @param areaId 入参
     * @return ResponseDTO<String>
     */
    @Transactional(rollbackFor = Exception.class)
    public ResponseDTO<String> delete(Long areaId) {
        if (null == areaId) {
            return ResponseDTO.userErrorParam(UserErrorCode.PARAM_ERROR.getMsg());
        }

        AreaEntity area = areaManager.queryArea(areaId);
        if (!locationQueryService.checkLocationExist(areaId)) {
            return ResponseDTO.userErrorParam(area.getAreaName() + "已关联库位,无法删除");
        }

        areaDao.deleteById(areaId);

        //更新缓存
        areaManager.removeCache();
        return ResponseDTO.ok();
    }


}
