package io.renren.modules.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.Query;
import io.renren.modules.business.dao.BusinessAnnualDao;
import io.renren.modules.business.dao.BusinessOutlayDao;
import io.renren.modules.business.dao.BusinessOutlayIncomeDao;
import io.renren.modules.business.entity.BusinessAnnualEntity;
import io.renren.modules.business.service.BusinessAnnualService;
import org.springframework.beans.factory.annotation.Autowired;
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;
import java.util.Map;


@Service("businessAnnualService")
public class BusinessAnnualServiceImpl extends ServiceImpl<BusinessAnnualDao, BusinessAnnualEntity> implements BusinessAnnualService {

    @Autowired
    BusinessOutlayDao businessOutlayDao;//明细表信息
    @Autowired
    BusinessOutlayIncomeDao businessOutlayIncomeDao;//总结表信息

    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<BusinessAnnualEntity> page = this.page(
                new Query<BusinessAnnualEntity>().getPage(params),
                new QueryWrapper<BusinessAnnualEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * 列表获取所有列表 无翻页
     */
    @Override
    public List<BusinessAnnualEntity> queryList(Map<String, Object> params) {
        return baseMapper.queryList(params);
    }

    /**
     * 获取明细表总结表填写人信息
     * @param params
     * @return
     */
    @Override
    public List<BusinessAnnualDao> getWriteList() {
        return baseMapper.getWriteList();
    }

    /**
     * 更新年度总结表和对应的关联表对应的年度
     * @param businessAnnual
     */
    @Override
    public void updateByIdNew(BusinessAnnualEntity businessAnnual) {
        // 明细表处理
        if(businessAnnual.getAnnualName() == 0){
            String startTime = businessAnnual.getAnnualYear() + "-01-01";
            String endTime = businessAnnual.getAnnualYear() + "-12-31";
            businessOutlayDao.updateND(startTime,endTime,businessAnnual.getAnnualHsId().toString());
        }else{ // 总结表处理
            String startTime = businessAnnual.getAnnualYear() + "-01-01";
            String endTime = businessAnnual.getAnnualYear() + "-12-31";
            businessOutlayIncomeDao.updateND(startTime,endTime,businessAnnual.getAnnualHsId().toString());
        }
        baseMapper.updateById(businessAnnual);
    }

    /**
     * 判断是否保存到年度表中
     * @param oiDate
     * @param writeUserId
     * @param i
     */
    @Override
    public void ifSave(Date oiDate, String writeUserId, int i) throws ParseException {
        // 获取当前年份
        Calendar c = Calendar.getInstance();
        //当前时间的年
        int year = Integer.parseInt(String.valueOf(c.get(Calendar.YEAR)));
        //判断的时间节点为 每年的3月1号
        String ifTime = year + "-03-01";
        // 装换时间用
        SimpleDateFormat ft = new SimpleDateFormat("yyyy-MM-dd");
        SimpleDateFormat jt = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //  格式化 传入的时间   到年
        String yzTime = ft.format(oiDate).substring(0,4);
        // 时间比较 -1  前面的时间比后面的小   0 则前面的大 1则为相等
        if(oiDate.compareTo(ft.parse(ifTime)) < 0){ //   说明传入的时间比每年的3月1号小 如果有当前年份则不需要添加
            if(!(baseMapper.countYear(yzTime,writeUserId,String.valueOf(i)) > 0)){ //说明没有数据  则添加
                BusinessAnnualEntity businessAnnualEntity = new BusinessAnnualEntity();
                businessAnnualEntity.setAnnualYear(yzTime);
                businessAnnualEntity.setAnnualName(i);
                businessAnnualEntity.setAnnualState(0);
                businessAnnualEntity.setAnnualHsId(Integer.parseInt(writeUserId));
                businessAnnualEntity.setAnnualEndtime(jt.parse(jt.format(c.getTime())));
                baseMapper.newSave(businessAnnualEntity);
            }
        } else { // 大或者相等  第二年  如果没有则添加年份
            if(!(baseMapper.countYear(yzTime,writeUserId,String.valueOf(i)) > 0)){
                BusinessAnnualEntity businessAnnualEntity = new BusinessAnnualEntity();
                businessAnnualEntity.setAnnualYear(String.valueOf(Integer.parseInt(yzTime) + 1));
                businessAnnualEntity.setAnnualName(i);
                businessAnnualEntity.setAnnualState(0);
                businessAnnualEntity.setAnnualHsId(Integer.parseInt(writeUserId));
                businessAnnualEntity.setAnnualEndtime(jt.parse(jt.format(c.getTime())));
                baseMapper.newSave(businessAnnualEntity);
            }
        }
    }

}