package com.srs.dormitory.service.impl;

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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.common.core.domain.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import com.srs.dormitory.mapper.DmsNewRangeMapper;
import com.srs.dormitory.domain.DmsNewRange;
import com.srs.dormitory.service.IDmsNewRangeService;

/**
 * 住宿区间Service业务层处理
 *
 * @author srs
 * @date 2024-03-15
 */
@Service
public class DmsNewRangeServiceImpl extends ServiceImpl<DmsNewRangeMapper, DmsNewRange> implements IDmsNewRangeService {
    @Autowired
    private DmsNewRangeMapper dmsNewRangeMapper;

    /**
     * 查询住宿区间
     *
     * @param id 住宿区间主键
     * @return 住宿区间
     */
    @Override
    public DmsNewRange selectDmsNewRangeById(Long id) {
        return dmsNewRangeMapper.selectDmsNewRangeById(id);
    }

    /**
     * 查询住宿区间列表
     *
     * @param dmsNewRange 住宿区间
     * @return 住宿区间
     */
    @Override
    public List<DmsNewRange> selectDmsNewRangeList(DmsNewRange dmsNewRange) {
        return dmsNewRangeMapper.selectDmsNewRangeList(dmsNewRange);
    }

    /**
     * 新增住宿区间
     *
     * @param dmsNewRange 住宿区间
     * @return 结果
     */
    @Override
    public AjaxResult insertDmsNewRange(DmsNewRange dmsNewRange) {
        try {
            String rangeCode = dmsNewRange.getRangeCode();
            if (rangeCode.isEmpty()) {
                throw new Exception("区间代码不能为空");
            }

            QueryWrapper<DmsNewRange> query1 = new QueryWrapper<>();
            query1.eq("range_status",'1');
            List<DmsNewRange> list1 = dmsNewRangeMapper.selectList(query1);
            if(list1.size() >0 && Objects.equals(dmsNewRange.getRangeStatus(), "1")){
                throw new Exception("只能启用一个区间");
            }

            QueryWrapper<DmsNewRange> query = new QueryWrapper<>();
            query.eq("range_code", rangeCode);

            List<DmsNewRange> list = dmsNewRangeMapper.selectList(query);
            if (list.size() != 0) {
                throw new Exception("区间代码已存在，换一个吧");
            }

            int res = dmsNewRangeMapper.insertDmsNewRange(dmsNewRange);
            if (res == 0) {
                throw new Exception("插入记录失败");
            }
            return AjaxResult.success();
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }

    }

    /**
     * 修改住宿区间
     *
     * @param dmsNewRange 住宿区间
     * @return 结果
     */
    @Override
    public AjaxResult updateDmsNewRange(DmsNewRange dmsNewRange) {
        try {
            String rangeCode = dmsNewRange.getRangeCode();
            if (rangeCode.isEmpty()) {
                throw new Exception("区间代码不能为空");
            }

            QueryWrapper<DmsNewRange> query1 = new QueryWrapper<>();
            query1.eq("range_status",'1')
                    .ne("id",dmsNewRange.getId());
            List<DmsNewRange> list1 = dmsNewRangeMapper.selectList(query1);
            if(list1.size() >0 && Objects.equals(dmsNewRange.getRangeStatus(), "1")){
                throw new Exception("只能启用一个区间");
            }

            QueryWrapper<DmsNewRange> query = new QueryWrapper<>();
            query.eq("range_code", rangeCode)
                    .ne("id",dmsNewRange.getId());

            List<DmsNewRange> list = dmsNewRangeMapper.selectList(query);
            if (list.size() != 0) {
                throw new Exception("区间代码冲突，换一个吧");
            }

            int res = dmsNewRangeMapper.updateDmsNewRange(dmsNewRange);
            if (res == 0) {
                throw new Exception("修改记录失败");
            }
            return AjaxResult.success();
        } catch (Exception ex) {
            return AjaxResult.error(ex.getMessage());
        }
    }

    /**
     * 批量删除住宿区间
     *
     * @param ids 需要删除的住宿区间主键
     * @return 结果
     */
    @Override
    public int deleteDmsNewRangeByIds(Long[] ids) {
        return dmsNewRangeMapper.deleteDmsNewRangeByIds(ids);
    }

    /**
     * 删除住宿区间信息
     *
     * @param id 住宿区间主键
     * @return 结果
     */
    @Override
    public int deleteDmsNewRangeById(Long id) {
        return dmsNewRangeMapper.deleteDmsNewRangeById(id);
    }
}
