package com.cool.store.dao.plan;

import com.cool.store.entity.plan.CityPlanningDO;
import com.cool.store.mapper.plan.CityPlanningMapper;
import com.cool.store.request.plan.CityPlanningQueryRequest;
import lombok.RequiredArgsConstructor;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;
import tk.mybatis.mapper.entity.Example;

import java.util.List;
import java.util.Objects;

/**
 * 城市规划DAO
 */
@Repository
@RequiredArgsConstructor
public class CityPlanningDAO {
    private final CityPlanningMapper cityPlanningMapper;

    /**
     * 新增
     */
    public boolean insertSelective(CityPlanningDO cityPlanningDO) {
        return cityPlanningMapper.insertSelective(cityPlanningDO) > 0;
    }

    /**
     * 编辑
     */
    public boolean updateSelective(CityPlanningDO cityPlanningDO) {
        return cityPlanningMapper.updateByPrimaryKeySelective(cityPlanningDO) > 0;
    }

    /**
     * 根据id查询
     */
    public CityPlanningDO getById(Long id) {
        return cityPlanningMapper.selectByPrimaryKey(id);
    }

    /**
     * 批量删除
     */
    public boolean deleteByIds(List<Long> ids) {
        if (CollectionUtils.isEmpty(ids)) {
            return false;
        }
        Example example = new Example(CityPlanningDO.class);
        example.createCriteria().andIn("id", ids);
        return cityPlanningMapper.deleteByExample(example) > 0;
    }

    /**
     * 检查同年份同季度同省市是否存在规划
     * @param year 年份
     * @param quarter 季度
     * @param provinceCode 省区划代码
     * @param cityCode 市区划代码
     * @param excludeId 排除的id（用于编辑时排除自身）
     * @return 是否存在
     */
    public boolean existsByYearAndQuarterAndRegion(Integer year, Integer quarter,
                                                    String provinceCode, String cityCode, Long excludeId) {
        Example example = new Example(CityPlanningDO.class);
        Example.Criteria criteria = example.createCriteria();
        criteria.andEqualTo("year", year)
                .andEqualTo("quarter", quarter)
                .andEqualTo("provinceCode", provinceCode)
                .andEqualTo("cityCode", cityCode);
        if (Objects.nonNull(excludeId)) {
            criteria.andNotEqualTo("id", excludeId);
        }
        return cityPlanningMapper.selectCountByExample(example) > 0;
    }

    /**
     * 分页查询
     */
    public List<CityPlanningDO> queryByCondition(CityPlanningQueryRequest request) {
        Example example = new Example(CityPlanningDO.class);
        Example.Criteria criteria = example.createCriteria();

        if (StringUtils.isNotBlank(request.getPlanNo())) {
            criteria.andLike("planNo", "%" + request.getPlanNo() + "%");
        }
        if (Objects.nonNull(request.getYear())) {
            criteria.andEqualTo("year", request.getYear());
        }
        if (Objects.nonNull(request.getQuarter())) {
            criteria.andEqualTo("quarter", request.getQuarter());
        }
        if (StringUtils.isNotBlank(request.getProvinceCode())) {
            criteria.andEqualTo("provinceCode", request.getProvinceCode());
        }
        if (StringUtils.isNotBlank(request.getCityCode())) {
            criteria.andEqualTo("cityCode", request.getCityCode());
        }

        example.setOrderByClause("create_time DESC");
        return cityPlanningMapper.selectByExample(example);
    }
}

