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

import com.project.zcustom.domain.addional.LargeIssue;
import com.project.zcustom.mapper.unit.LargeIssueMapper;
import com.project.zcustom.service.unit.ILargeIssueService;
import com.project.common.core.domain.search.PageParamVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.project.common.utils.StringUtils;

import java.util.Date;
import java.util.List;

/**
 * 上报问题Service业务层处理
 *
 * @author change
 * @date 2024-12-30
 */
@Service
public class LargeIssueServiceImpl extends ServiceImpl<LargeIssueMapper, LargeIssue> implements ILargeIssueService {

    @Autowired
    private LargeIssueMapper largeIssueMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargeIssue> getLambdaQueryWrapper(LargeIssue largeIssue) {
        LambdaQueryWrapper<LargeIssue> lqw = new LambdaQueryWrapper<LargeIssue>();
                        if (StringUtils.isNotBlank(largeIssue.getDescription())){
                        lqw.eq(LargeIssue::getDescription ,largeIssue.getDescription());
                    }
                        if (StringUtils.isNotNull(largeIssue.getProjectId())){
                        lqw.eq(LargeIssue::getProjectId ,largeIssue.getProjectId());
                    }
                        if (StringUtils.isNotBlank(largeIssue.getStatus())){
                        lqw.eq(LargeIssue::getStatus ,largeIssue.getStatus());
                    }
        return lqw;
    }

    /**
     * 分页查询上报问题列表
     *
     * @param largeIssue 上报问题
     * @return 上报问题集合
     */
    @Override
    public IPage<LargeIssue> pagingLargeIssueList(LargeIssue largeIssue) {
        IPage<LargeIssue> page = new Page<>(largeIssue.getPageNum(), largeIssue.getPageSize());
        LambdaQueryWrapper<LargeIssue> lqw = getLambdaQueryWrapper(largeIssue);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeIssue::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 分页查询上报问题列表
     *
     * @param lqw
     * @return 上报问题集合
     */
    @Override
    public IPage<LargeIssue> pagingLargeIssueList(PageParamVo pageParamVo, LambdaQueryWrapper<LargeIssue> lqw) {
        IPage<LargeIssue> page = new Page<>(pageParamVo.getPageNum(), pageParamVo.getPageSize());
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeIssue::getCreateTime);
        return baseMapper.selectPage(page, lqw);
    }

    /**
     * 查询上报问题列表
     *
     * @param largeIssue 上报问题
     * @return 上报问题
     */
    @Override
    public List<LargeIssue> selectLargeIssueList(LargeIssue largeIssue) {
        LambdaQueryWrapper<LargeIssue> lqw = getLambdaQueryWrapper(largeIssue);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeIssue::getCreateTime);
        return baseMapper.selectList(lqw);
    }

    /**
     * 查询上报问题列表
     *
     * @param largeIssue 上报问题
     * @return 上报问题
     */
    @Override
    public List<LargeIssue> selectLargeIssueList(LargeIssue largeIssue,Long limitNum) {
        LambdaQueryWrapper<LargeIssue> lqw = getLambdaQueryWrapper(largeIssue);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargeIssue::getCreateTime);
        lqw.last("limit "+limitNum);
        return baseMapper.selectList(lqw);
    }

    /**
     * 根据指定字段查询信息
     *
     * @param field 字段名称
     * @param filedValue 字段值
     * @return 上报问题
     */
    @Override
    public LargeIssue getLargeIssueByField(String field, String filedValue) {
        QueryWrapper<LargeIssue> lqw = new QueryWrapper<LargeIssue>();
        lqw.eq(field, filedValue);
        lqw.last("limit 1");
        return this.getOne(lqw, false);
    }

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

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

    public int getIssueNum(String appOrg){
        return largeIssueMapper.getIssueNum(appOrg);
    }

    public int getOverIssueNum(String appOrg){
        return largeIssueMapper.getOverIssueNum(appOrg);
    }

    public List<LargeIssue> getIssueListLastWeek(String appOrg, Date date,Integer status){
        if (date == null){
            return largeIssueMapper.getIssueListLastWeek(appOrg,status);
        }
        return largeIssueMapper.getIssueList(appOrg, date,status);

    }


    public List<LargeIssue> selectList(LargeIssue largeIssue){
        return largeIssueMapper.selectLargeIssueList(largeIssue);
    }

    public LargeIssue selectLargeIssueById(Long id){
        return largeIssueMapper.selectLargeIssueById(id);
    }
}
