package com.huimv.management.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.huimv.common.exception.RRException;
import com.huimv.management.dao.UnitDao;
import com.huimv.management.entity.PeriodEntity;
import com.huimv.management.entity.PeriodEntity;
import com.huimv.management.entity.UnitEntity;
import com.huimv.management.entity.vo.PeriodVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.PageUtils;
import com.huimv.common.utils.Query;

import com.huimv.management.dao.PeriodDao;
import com.huimv.management.entity.PeriodEntity;
import com.huimv.management.service.PeriodService;
import org.springframework.transaction.annotation.Transactional;

import javax.validation.constraints.NotBlank;


@Service("periodService")
public class PeriodServiceImpl extends ServiceImpl<PeriodDao, PeriodEntity> implements PeriodService {

    @Autowired
    private PeriodDao periodDao;

    @Autowired
    private UnitDao unitDao;

    @Override
    public PageUtils queryPage(Map<String, Object> params) {

        QueryWrapper<PeriodVo> queryWrapper = new QueryWrapper<>();

        String farmId = (String) params.get("formFarmId");
        if (StringUtils.isBlank(farmId)) {
            throw new RRException("参数有误,请检查!",1001);
        }
        queryWrapper.eq("period.farm_id",Integer.parseInt(farmId));

        String pigstyId = (String) params.get("pigstyId");
        if (StringUtils.isNotBlank(pigstyId)) {
            queryWrapper.eq("period.pigsty_id",Integer.parseInt(pigstyId));
        }

        String unitId = (String) params.get("unitId");
        if (StringUtils.isNotBlank(unitId)) {
            queryWrapper.eq("period.unit_id",unitId);
        }

        String keyword = (String) params.get("keyword");
        if (StringUtils.isNotBlank(keyword)) {
            queryWrapper.like("period.number", keyword);
        }

        queryWrapper.apply("period.deleted = 0 AND pigsty.deleted = 0 AND unit.deleted = 0");
        IPage<PeriodVo> page = baseMapper.page(new Query<>().getPage(params),queryWrapper);
        return new PageUtils(page);
    }

    @Override
    public List findAll(Integer formFarmId) {
        LambdaQueryWrapper<PeriodEntity> lambdaQuery = Wrappers.lambdaQuery();
        lambdaQuery.eq(PeriodEntity::getFarmId,formFarmId);
        List<PeriodEntity> farmEntities = periodDao.selectList(lambdaQuery);
        return farmEntities;
    }

    @Override
    public List findAllByFarmId(Integer id) {
        LambdaQueryWrapper<PeriodEntity> lambdaQuery = Wrappers.lambdaQuery();
        List<PeriodEntity> periodEntities = new ArrayList<>();
        if (id != null){
             lambdaQuery.eq(PeriodEntity::getPigstyId, id);
            periodEntities = periodDao.selectList(lambdaQuery);
        }

        return periodEntities;
    }


    @Override
    @Transactional(rollbackFor = Throwable.class)
    public void insert(PeriodEntity period) {

        StringBuilder sb = new StringBuilder();
        String periodId = sb.append(period.getFarmId()).append(period.getPigstyId()).append(period.getUnitId()).append(DateUtil.format(new Date(), "yyyyMMdd")).toString();
        period.setNumber(periodId);
        Integer unitId = period.getUnitId();
        UnitEntity unitEntity = unitDao.selectById(unitId);
        if (unitEntity != null) {
            Integer currentPeriodId = unitEntity.getCurrentPeriodId();
            if (currentPeriodId != null) {
                throw new RRException("当前单元已存在栏期!",1001);
            }

            baseMapper.insert(period);

            LambdaUpdateWrapper<UnitEntity> updateWrapper = Wrappers.lambdaUpdate();
            updateWrapper.eq(UnitEntity::getId,unitId)
                    .set(UnitEntity::getCurrentPeriodId,period.getId());

            unitDao.update(null,updateWrapper);

        }
    }
}