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.controller.core.StringUtils;
import com.project.zcustom.domain.addional.LargeProject;
import com.project.zcustom.mapper.unit.LargeProjectMapper;
import com.project.zcustom.service.unit.ILargeProjectService;
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 LargeProjectServiceImpl extends ServiceImpl<LargeProjectMapper, LargeProject> implements ILargeProjectService {

    @Autowired
    private LargeProjectMapper largeProjectMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeProject> getLambdaQueryWrapper(LargeProject largeProject) {
        LambdaQueryWrapper<LargeProject> lqw = new LambdaQueryWrapper<LargeProject>();
                        if (StringUtils.isNotBlank(largeProject.getProjectName())){
                        lqw.like(LargeProject::getProjectName ,largeProject.getProjectName());
                    }
                        if (StringUtils.isNotNull(largeProject.getUnitCode())){
                        lqw.eq(LargeProject::getUnitCode ,largeProject.getUnitCode());
                    }
                        if (StringUtils.isNotBlank(largeProject.getConnectFlag())){
                        lqw.eq(LargeProject::getConnectFlag ,largeProject.getConnectFlag());
                    }
        return lqw;
    }

    /**
     * 分页查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目集合
     */
    @Override
    public IPage<LargeProject> pagingLargeProjectList(LargeProject largeProject) {
        IPage<LargeProject> page = new Page<>(largeProject.getPageNum(), largeProject.getPageSize());
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询工程项目列表
     *
     * @param lqw
     * @return 工程项目集合
     */
    @Override
    public IPage<LargeProject> pagingLargeProjectList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeProject> lqw) {
        IPage<LargeProject> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectList(LargeProject largeProject) {
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询工程项目列表
     *
     * @param largeProject 工程项目
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectList(LargeProject largeProject,Long limitNum) {
        LambdaQueryWrapper<LargeProject> lqw = getLambdaQueryWrapper(largeProject);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeProject::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 工程项目
     */
    @Override
    public LargeProject getLargeProjectByField(String field, String filedValue) {
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

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

    /**
     * 根据指定字段查询信息列表
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 工程项目
     */
    @Override
    public List<LargeProject> selectLargeProjectListByField(String field, String filedValue) {
        QueryWrapper<LargeProject> lqw = new QueryWrapper<LargeProject>();
        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<LargeProject> lqw = new QueryWrapper<LargeProject>();
        lqw.eq(field, filedValue);
        lqw.ne("id", id);
        if(this.getOne(lqw,false) != null){
            throw new RuntimeException(tips + "已存在");
        }
    }
    /**
     *  真删接口
     */
    @Override
    public void reallyDeleteLargeProject(LambdaQueryWrapper<LargeProject> lqw){
        baseMapper.reallyDeleteByWrapper(lqw);
    }

    public int getProjectNumX(String subdivision) {
        return largeProjectMapper.getProjectNumX(subdivision);
    }
    public int getProjectNumY(String subdivision) {
        return largeProjectMapper.getProjectNumY(subdivision);
    }

    public List<LargeProject> getCameraNumList(String unitCode){
        return largeProjectMapper.getCameraNumList(unitCode);
    }

    public int getProjectNumThisWeek(){
        return largeProjectMapper.getProjectNumThisWeek();
    }
}
