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

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

import java.util.ArrayList;
import java.util.List;

@Service
public class AreaServiceImpl implements AreaService {

    @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);
        areaManager.save(areaEntity);
        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);
        //更新并更新缓存
        areaManager.update(areaEntity);
        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 resultMsg = JoinerResult.createJoiner();
        List<Long> idsToDelete = new ArrayList<>();
        for (Long id : idList) {
            AreaEntity area = areaManager.queryArea(id);
            if (!locationQueryService.checkLocationExist(id)) {
                resultMsg.getErrorMsg().add(area.getAreaName() + "已关联库位,无法删除");
            } else {
                idsToDelete.add(id);
                resultMsg.getSussMsg().add(area.getAreaName() + "删除成功");
            }
        }

        if (CollectionUtils.isNotEmpty(idsToDelete)) {
            //删除并删除缓存
            areaManager.batchDelete(idsToDelete);
        }

        return ResponseDTOUtil.buildResponseDTO(resultMsg);
    }

    /**
     * 单个删除
     *
     * @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() + "已关联库位,无法删除");
        }

        //单个删除并删除缓存
        areaManager.deleteById(areaId);
        return ResponseDTO.ok();
    }

}
