package com.jeesharp.business.modules.mall.service.impl;

import com.jeesharp.business.modules.mall.entity.MallPickupPoint;
import com.jeesharp.business.modules.mall.enums.MallPickupPointStatusEnum;
import com.jeesharp.business.modules.mall.mapper.MallPickupPointMapper;
import com.jeesharp.business.modules.mall.service.MallPickupPointService;
import com.jeesharp.business.modules.mall.vo.MallPickupPointQueryVO;
import com.jeesharp.framework.mybatis.core.service.CrudService;
import cn.hutool.core.util.StrUtil;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

@Service
@Transactional(readOnly = true)
public class MallPickupPointServiceImpl extends CrudService<MallPickupPointMapper, MallPickupPoint, MallPickupPointQueryVO> implements MallPickupPointService {

    private static final String IDS_SEPARATOR = ",";

    @Override
    @Transactional(readOnly = false)
    public void createPoint(MallPickupPoint entity) {
        if (entity.getStatus() == null) {
            entity.setStatus(MallPickupPointStatusEnum.ENABLED.getValue());
        }
        save(entity);
    }

    @Override
    @Transactional(readOnly = false)
    public void updatePoint(MallPickupPoint entity) {
        update(entity);
    }

    @Override
    public List<MallPickupPoint> listEnabledPoints() {
        MallPickupPointQueryVO query = new MallPickupPointQueryVO();
        query.setStatus(MallPickupPointStatusEnum.ENABLED.getValue());
        return findList(query);
    }

    @Override
    public List<MallPickupPoint> getPointsByRegion(String province, String city, String district) {
        MallPickupPointQueryVO query = new MallPickupPointQueryVO();
        if (StrUtil.isNotBlank(province)) {
            query.setProvince(province.trim());
        }
        if (StrUtil.isNotBlank(city)) {
            query.setCity(city.trim());
        }
        if (StrUtil.isNotBlank(district)) {
            query.setDistrict(district.trim());
        }
        query.setStatus(MallPickupPointStatusEnum.ENABLED.getValue());
        return findList(query);
    }
}
