package org.dromara.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.utils.MapstructUtils;
import org.dromara.common.core.utils.StringUtils;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.domain.assets.CommunityAssets;
import org.dromara.domain.assets.bo.CommunityAssetsBo;
import org.dromara.domain.assets.vo.CommunityAssetsVo;
import org.dromara.mapper.CommunityAssetsMapper;
import org.dromara.service.ICommunityAssetsService;
import org.springframework.stereotype.Service;

import java.util.Collection;
import java.util.List;
import java.util.Map;

/**
 * 社区资产Service业务层处理
 *
 * @author boman
 * @date 2025-04-11
 */
@RequiredArgsConstructor
@Service
public class CommunityAssetsServiceImpl implements ICommunityAssetsService {

    private final CommunityAssetsMapper baseMapper;

    /**
     * 查询社区资产
     *
     * @param communityAssetId 主键
     * @return 社区资产
     */
    @Override
    public CommunityAssetsVo queryById(Long communityAssetId){
        return baseMapper.selectVoById(communityAssetId);
    }

    /**
     * 分页查询社区资产列表
     *
     * @param bo        查询条件
     * @param pageQuery 分页参数
     * @return 社区资产分页列表
     */
    @Override
    public TableDataInfo<CommunityAssetsVo> queryPageList(CommunityAssetsBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<CommunityAssets> lqw = buildQueryWrapper(bo);
        Page<CommunityAssetsVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询符合条件的社区资产列表
     *
     * @param bo 查询条件
     * @return 社区资产列表
     */
    @Override
    public List<CommunityAssetsVo> queryList(CommunityAssetsBo bo) {
        LambdaQueryWrapper<CommunityAssets> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<CommunityAssets> buildQueryWrapper(CommunityAssetsBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<CommunityAssets> lqw = Wrappers.lambdaQuery();
        lqw.orderByAsc(CommunityAssets::getCommunityAssetId);
        lqw.eq(bo.getAssetId() != null, CommunityAssets::getAssetId, bo.getAssetId());
        lqw.like(StringUtils.isNotBlank(bo.getAssetName()), CommunityAssets::getAssetName, bo.getAssetName());
        lqw.like(StringUtils.isNotBlank(bo.getEquipmentName()), CommunityAssets::getEquipmentName, bo.getEquipmentName());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentCoverPhoto()), CommunityAssets::getEquipmentCoverPhoto, bo.getEquipmentCoverPhoto());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentNumber()), CommunityAssets::getEquipmentNumber, bo.getEquipmentNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentQrCode()), CommunityAssets::getEquipmentQrCode, bo.getEquipmentQrCode());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentStatus()), CommunityAssets::getEquipmentStatus, bo.getEquipmentStatus());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentModel()), CommunityAssets::getEquipmentModel, bo.getEquipmentModel());
        lqw.eq(bo.getPurchaseDate() != null, CommunityAssets::getPurchaseDate, bo.getPurchaseDate());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentPurpose()), CommunityAssets::getEquipmentPurpose, bo.getEquipmentPurpose());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentLocation()), CommunityAssets::getEquipmentLocation, bo.getEquipmentLocation());
        lqw.eq(bo.getEquipmentPrice() != null, CommunityAssets::getEquipmentPrice, bo.getEquipmentPrice());
        lqw.eq(bo.getStorageTime() != null, CommunityAssets::getStorageTime, bo.getStorageTime());
        lqw.eq(StringUtils.isNotBlank(bo.getStoragePerson()), CommunityAssets::getStoragePerson, bo.getStoragePerson());
        lqw.eq(bo.getOutboundTime() != null, CommunityAssets::getOutboundTime, bo.getOutboundTime());
        lqw.eq(StringUtils.isNotBlank(bo.getOutboundPerson()), CommunityAssets::getOutboundPerson, bo.getOutboundPerson());
        lqw.eq(StringUtils.isNotBlank(bo.getPlateNumber()), CommunityAssets::getPlateNumber, bo.getPlateNumber());
        lqw.eq(StringUtils.isNotBlank(bo.getEquipmentPhotos()), CommunityAssets::getEquipmentPhotos, bo.getEquipmentPhotos());
        lqw.eq(bo.getMaintenanceDate() != null, CommunityAssets::getMaintenanceDate, bo.getMaintenanceDate());
        lqw.eq(StringUtils.isNotBlank(bo.getMaintenancePerson()), CommunityAssets::getMaintenancePerson, bo.getMaintenancePerson());
        lqw.eq(StringUtils.isNotBlank(bo.getWarrantyPeriod()), CommunityAssets::getWarrantyPeriod, bo.getWarrantyPeriod());
        lqw.eq(StringUtils.isNotBlank(bo.getDepreciationRate()), CommunityAssets::getDepreciationRate, bo.getDepreciationRate());
        return lqw;
    }

    /**
     * 新增社区资产
     *
     * @param bo 社区资产
     * @return 是否新增成功
     */
    @Override
    public Boolean insertByBo(CommunityAssetsBo bo) {
        CommunityAssets add = MapstructUtils.convert(bo, CommunityAssets.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setCommunityAssetId(add.getCommunityAssetId());
        }
        return flag;
    }

    /**
     * 修改社区资产
     *
     * @param bo 社区资产
     * @return 是否修改成功
     */
    @Override
    public Boolean updateByBo(CommunityAssetsBo bo) {
        CommunityAssets update = MapstructUtils.convert(bo, CommunityAssets.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(CommunityAssets entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 校验并批量删除社区资产信息
     *
     * @param ids     待删除的主键集合
     * @param isValid 是否进行有效性校验
     * @return 是否删除成功
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteByIds(ids) > 0;
    }

    @Override
    public boolean checkEquipmentNumberUnique(CommunityAssetsBo bo) {
        Long communityAssetId = StringUtils.isNull(bo.getCommunityAssetId()) ? -1L : bo.getCommunityAssetId();
        CommunityAssetsVo info = baseMapper.checkEquipmentNumberUnique(bo.getEquipmentNumber());
        if (StringUtils.isNotNull(info) && info.getCommunityAssetId().longValue() != communityAssetId.longValue())
        {
            return false;
        }
        return true;
    }
}
