package com.project.zcustom.service.unit.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.zcustom.controller.core.PageParamVo;
import com.project.zcustom.domain.addional.LargeUnit;
import com.project.zcustom.mapper.unit.LargeUnitMapper;
import com.project.zcustom.service.unit.ILargeUnitService;
import com.project.zcustom.controller.core.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 地区编码Service业务层处理
 *
 * @author change
 * @date 2024-12-30
 */
@Service
public class LargeUnitServiceImpl extends ServiceImpl<LargeUnitMapper, LargeUnit> implements ILargeUnitService {

    @Autowired
    private LargeUnitMapper largeUnitMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeUnit> getLambdaQueryWrapper(LargeUnit largeUnit) {
        LambdaQueryWrapper<LargeUnit> lqw = new LambdaQueryWrapper<LargeUnit>();
                        if (StringUtils.isNotBlank(largeUnit.getUnitCode())){
                        lqw.eq(LargeUnit::getUnitCode ,largeUnit.getUnitCode());
                    }
                        if (StringUtils.isNotBlank(largeUnit.getUnitName())){
                        lqw.like(LargeUnit::getUnitName ,largeUnit.getUnitName());
                    }
        return lqw;
    }

    /**
     * 分页查询地区编码列表
     *
     * @param largeUnit 地区编码
     * @return 地区编码集合
     */
    @Override
    public IPage<LargeUnit> pagingLargeUnitList(LargeUnit largeUnit) {
        IPage<LargeUnit> page = new Page<>(largeUnit.getPageNum(), largeUnit.getPageSize());
        LambdaQueryWrapper<LargeUnit> lqw = getLambdaQueryWrapper(largeUnit);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeUnit::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询地区编码列表
     *
     * @param lqw
     * @return 地区编码集合
     */
    @Override
    public IPage<LargeUnit> pagingLargeUnitList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeUnit> lqw) {
        IPage<LargeUnit> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeUnit::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询地区编码列表
     *
     * @param largeUnit 地区编码
     * @return 地区编码
     */
    @Override
    public List<LargeUnit> selectLargeUnitList(LargeUnit largeUnit) {
        LambdaQueryWrapper<LargeUnit> lqw = getLambdaQueryWrapper(largeUnit);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeUnit::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询地区编码列表
     *
     * @param largeUnit 地区编码
     * @return 地区编码
     */
    @Override
    public List<LargeUnit> selectLargeUnitList(LargeUnit largeUnit,Long limitNum) {
        LambdaQueryWrapper<LargeUnit> lqw = getLambdaQueryWrapper(largeUnit);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeUnit::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 地区编码
     */
    @Override
    public LargeUnit getLargeUnitByField(String field, String filedValue) {
        QueryWrapper<LargeUnit> lqw = new QueryWrapper<LargeUnit>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 地区编码
     */
    @Override
    public List<LargeUnit> selectLargeUnitListByField(String field, String filedValue,Long limitNum) {
        QueryWrapper<LargeUnit> lqw = new QueryWrapper<LargeUnit>();
        lqw.eq(field, filedValue);
        lqw.last("limit "+limitNum);
        return this.list(lqw);
    }

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 地区编码
     */
    @Override
    public List<LargeUnit> selectLargeUnitListByField(String field, String filedValue) {
        QueryWrapper<LargeUnit> lqw = new QueryWrapper<LargeUnit>();
        lqw.eq(field, filedValue);
        return this.list(lqw);
    }

    /**
     * 判断指定字段值是否存在
     *
     * @param field 字段名称
     * @param filedValue 字段值
     */
    @Override
    public void checkFieldExist(String field, String filedValue,String tips, Long id){
        QueryWrapper<LargeUnit> lqw = new QueryWrapper<LargeUnit>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
    /**
     *  真删接口
     */
    @Override
    public void reallyDeleteLargeUnit(LambdaQueryWrapper<LargeUnit> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    public List<LargeUnit> getProjectCameraNumList(){
        return largeUnitMapper.getProjectCameraNumList();
    }

    public int getConnectUnitNum(){
        return largeUnitMapper.getConnectUnitNum();
    }

    public List<LargeUnit> getAllCameraCondition(){
        return largeUnitMapper.getAllCameraCondition();
    }

    public LargeUnit getCameraConditionByAppOrg(String appOrg){
        return largeUnitMapper.getCameraConditionByAppOrg(appOrg);
    }
}
