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

import com.project.zcustom.domain.addional.LargePlan;
import com.project.zcustom.mapper.unit.LargePlanMapper;
import com.project.zcustom.service.unit.ILargePlanService;
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 LargePlanServiceImpl extends ServiceImpl<LargePlanMapper, LargePlan> implements ILargePlanService {

    @Autowired
    private LargePlanMapper largePlanMapper;

    /**
     * 组装查询条件
     */
    private LambdaQueryWrapper<LargePlan> getLambdaQueryWrapper(LargePlan largePlan) {
        LambdaQueryWrapper<LargePlan> lqw = new LambdaQueryWrapper<LargePlan>();
                        if (StringUtils.isNotNull(largePlan.getProjectId())){
                        lqw.eq(LargePlan::getProjectId ,largePlan.getProjectId());
                    }
                        if (StringUtils.isNotBlank(largePlan.getType())){
                        lqw.eq(LargePlan::getType ,largePlan.getType());
                    }
                        if (StringUtils.isNotBlank(largePlan.getHead())){
                        lqw.eq(LargePlan::getHead ,largePlan.getHead());
                    }
                        if (StringUtils.isNotBlank(largePlan.getContent())){
                        lqw.eq(LargePlan::getContent ,largePlan.getContent());
                    }
                        if (StringUtils.isNotNull(largePlan.getStartTime())){
                        lqw.eq(LargePlan::getStartTime ,largePlan.getStartTime());
                    }
                        if (StringUtils.isNotNull(largePlan.getEndTime())){
                        lqw.eq(LargePlan::getEndTime ,largePlan.getEndTime());
                    }
        return lqw;
    }

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

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

    /**
     * 查询工程计划列表
     *
     * @param largePlan 工程计划
     * @return 工程计划
     */
    @Override
    public List<LargePlan> selectLargePlanList(LargePlan largePlan) {
        LambdaQueryWrapper<LargePlan> lqw = getLambdaQueryWrapper(largePlan);
        // 排序 默认时间倒序
        lqw.orderByDesc(LargePlan::getCreateTime);
        return baseMapper.selectList(lqw);
    }

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

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

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

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

    public int getCheckProjectNum(Date startDate, Date endDate){
        return largePlanMapper.getCheckProjectNum(startDate, endDate);
    }

    public List<LargePlan> planCheckList(){
        return largePlanMapper.planCheckList();
    }

    public List<LargePlan> planDoneList(){
        return largePlanMapper.planDoneList();
    }
}
