package com.gyee.runeconomy.service.auto.impl;

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.gyee.common.model.StringUtils;
import com.gyee.common.util.SortUtils;
import com.gyee.runeconomy.mapper.auto.ProBasicProjectPlanMapper;
import com.gyee.runeconomy.model.auto.ProBasicProjectPlan;
import com.gyee.runeconomy.service.auto.IProBasicProjectPlanService;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 工程计划表 服务实现类
 * </p>
 *
 * @author shilin
 * @since 2022-10-21
 */
@Service
public class ProBasicProjectPlanServiceImpl extends ServiceImpl<ProBasicProjectPlanMapper, ProBasicProjectPlan> implements IProBasicProjectPlanService {
    @Override
    public List<ProBasicProjectPlan> getProjectPlanList(String year, String pageNum, String pageSize) {
        Page<ProBasicProjectPlan> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        QueryWrapper<ProBasicProjectPlan> qw = new QueryWrapper<>();
        if (com.gyee.common.model.StringUtils.isNotEmpty(year)) {
            qw.lambda().eq(ProBasicProjectPlan::getYear, year);
        }
        IPage<ProBasicProjectPlan> ProBasicLine = getBaseMapper().selectPage(page, qw);
        List<ProBasicProjectPlan> list = ProBasicLine.getRecords();
        SortUtils.sort(list, "projectId", SortUtils.DESC);
        return list;
    }

    @Override
    public List<ProBasicProjectPlan> getList(String year) {
        QueryWrapper<ProBasicProjectPlan> qw = new QueryWrapper<>();
        if (StringUtils.isNotEmpty(year)) {
            qw.like("year", year);
        }
        List<ProBasicProjectPlan> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<ProBasicProjectPlan> getmonthList(String month) {
        QueryWrapper<ProBasicProjectPlan> qw = new QueryWrapper<>();
        Calendar cal = Calendar.getInstance();
        int currentyear = cal.get(Calendar.YEAR);
        if (StringUtils.isNotEmpty(month)) {
            qw.eq("month", month);
            qw.like("year", currentyear);
        }
        List<ProBasicProjectPlan> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public List<ProBasicProjectPlan> getmonthList1(String month, int currentyear) {
        QueryWrapper<ProBasicProjectPlan> qw = new QueryWrapper<>();
        Calendar cal = Calendar.getInstance();
        if (StringUtils.isNotEmpty(month)) {
            qw.eq("month", month);
            qw.like("year", currentyear);
        }
        List<ProBasicProjectPlan> list = baseMapper.selectList(qw);
        return list;
    }

    @Override
    public IPage<ProBasicProjectPlan> List(String id, String projectId, String windpowerstationId, String year, String month, String modificationTime, String pageNum, String pageSize) {
        QueryWrapper<ProBasicProjectPlan> qw = new QueryWrapper<>();

        if (StringUtils.isNotEmpty(id)) {
            qw.eq("id", id);
        }

        if (StringUtils.isNotEmpty(projectId)) {
            qw.like("project_id", projectId);
        }

        if (StringUtils.isNotEmpty(windpowerstationId)) {
            qw.like("windpowerstation_id", windpowerstationId);
        }

        if (StringUtils.isNotEmpty(year)) {
            qw.eq("year", year);
        }

        if (StringUtils.isNotEmpty(month)) {
            qw.eq("month", month);
        }
        if (StringUtils.isNotEmpty(modificationTime)) {
            try {
                Date beginDate = new SimpleDateFormat("yyyy-MM-dd").parse(modificationTime);
                qw.ge("modification_time", beginDate);
            } catch (ParseException e) {
                e.printStackTrace();
            }
        }
        Page<ProBasicProjectPlan> page = new Page<>(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
        IPage<ProBasicProjectPlan> ProjectPlanPage = getBaseMapper().selectPage(page, qw);
        return ProjectPlanPage;
    }
}
