package cn.com.victorysoft.business.condition.service;

import cn.com.victorysoft.business.condition.dao.AdjustPlanDao;
import cn.com.victorysoft.business.condition.dto.AdjustPlanSummary;
import cn.com.victorysoft.business.condition.dto.AdjustPlanSummaryType;
import cn.com.victorysoft.business.condition.entity.AdjustPlan;
import cn.com.victorysoft.business.condition.entity.Gzlx;
import cn.com.victorysoft.business.condition.param.AdjustPlanParam;
import cn.com.victorysoft.business.condition.param.GzlxParam;
import cn.com.victorysoft.business.sys.entity.WellBasic;
import cn.com.victorysoft.business.sys.param.*;
import cn.com.victorysoft.business.sys.service.SysBaseService;
import cn.com.victorysoft.business.util.PageUtils;
import cn.com.victorysoft.business.util.StringUtils;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

@Service
public class AdjustPlanService {

    @Resource
    private AdjustPlanDao adjustPlanDao;
    @Resource
    private SysBaseService sysBaseService;

    /**
     * 查询分页数据
     */
    public PageInfo<AdjustPlan> selectPage(AdjustPlanParam param) {
        PageUtils.startPage(param);
        return new PageInfo<>(this.selectList(param));
    }

    /**
     * 查询管柱类型
     */
    public List<Gzlx> selectGzlx(GzlxParam param) {
        return adjustPlanDao.selectGzlx(param);
    }

    /**
     * 查询列表数据
     */
    public List<AdjustPlan> selectList(AdjustPlanParam param) {
        return adjustPlanDao.selectList(param);
    }

    public int insert(AdjustPlan param) {
        WellBasic wellBasic = sysBaseService.selectWellBasic(param.getWellname());
        param.setUnitcode(wellBasic.getUnitcode());
        param.setCreatedOn(new Date());
        return adjustPlanDao.insert(param);
    }

    public AdjustPlan selectByPrimaryKey(AdjustPlan key) {
        return adjustPlanDao.selectByPrimaryKey(key);
    }

    public int update(AdjustPlan param) {
        param.setModifiedOn(new Date());
        return adjustPlanDao.updateByPrimaryKeySelective(param);
    }

    public int delete(AdjustPlan param) {
        return adjustPlanDao.deleteByPrimaryKey(param);
    }

    public int deleteByMonth(String month) {
        return adjustPlanDao.deleteByMonth(month);
    }

    public AdjustPlan generateByWellname(String wellname, String month) {
        AdjustPlan adjustPlan = adjustPlanDao.generateByWellname(wellname, month);
        if (adjustPlan == null) {
            adjustPlan = new AdjustPlan();
            adjustPlan.setWellname(wellname);
            adjustPlan.setMonth(month);
        }
        if (StringUtils.isEmpty(adjustPlan.getWellname())) {
            adjustPlan.setWellname(wellname);
        }
        return adjustPlan;
    }

    public List<AdjustPlanSummary> summary(UnitMonthParam param) {
        String unitColumn = sysBaseService.getUnitColumn(param.getUnitcode());
        return adjustPlanDao.summary(param, unitColumn);
    }

    public List<AdjustPlanSummaryType> summaryType(UnitMonthParam param) {
        String unitColumn = sysBaseService.getUnitColumn(param.getUnitcode());
        return adjustPlanDao.summaryType(param, unitColumn);
    }

}
