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

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.entity.BusinessOutlayIncomeEntity;
import io.renren.modules.business.service.BusinessOutlayIncomeService;
import io.renren.modules.sys.dao.SysUserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.math.BigDecimal;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;


@Service("businessOutlayIncomeService")
public class BusinessOutlayIncomeServiceImpl extends ServiceImpl<BusinessOutlayIncomeDao, BusinessOutlayIncomeEntity> implements BusinessOutlayIncomeService {

    @Autowired
    BusinessOutlayDao businessOutlayDao;

    @Autowired
    SysUserDao sysUserDao;
    @Autowired
    BusinessAnnualDao businessAnnualDao;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
//        IPage<BusinessOutlayIncomeEntity> page = this.page(
//                new Query<BusinessOutlayIncomeEntity>().getPage(params),
//                new QueryWrapper<BusinessOutlayIncomeEntity>()
//        );
        IPage<BusinessOutlayIncomeEntity> page = new Query<BusinessOutlayIncomeEntity>().getPage(params);
        List<BusinessOutlayIncomeEntity> list = baseMapper.listNowTime(params);
        page.setRecords(list);
        return new PageUtils(page);
    }


    /**
     * 获取未删除的列表不含有翻页
     */
    @Override
    public List<BusinessOutlayIncomeEntity> listNoPage(Map<String, Object> params) {
        return baseMapper.listNoPage(params);
    }


    @Override
    public PageUtils pcListPage(Map<String, Object> params) {
        IPage<BusinessOutlayIncomeEntity> page = new Query<BusinessOutlayIncomeEntity>().getPage(params);
        List<BusinessOutlayIncomeEntity> list = baseMapper.pcListNoPage(params);
        page.setRecords(list);
        return new PageUtils(page);
    }

    @Override
    public List<BusinessOutlayIncomeEntity> pcListNoPage(Map<String, Object> params){
        return baseMapper.pcListNoPage(params);
    }

    /**
     * 获取内部转账统计信息
     * @param params
     * @return
     */
    @Override
    public Map<String, Object> zclbList(Map<String, Object> params) {
//        //根据条件获取类别统计信息
//        List<Map<String,Object>> list = businessOutlayDao.getLbTotal(params);
//        //获取统计信息
//        List<Map<String,Object>> tj = baseMapper.zclbList(params);
//        //处理后的统计信息map
//        Map<String, Object> newMap = new HashMap<>();
//        //人员信息列表
//        List<SysUserEntity> ry = sysUserDao.sysUserlist();
//        Double total = 0.0;
//        for(Map<String, Object> i : tj){
//            //录入到map中
//            for(SysUserEntity j : ry){
//                if(i.get("id").toString().equals(j.getUserId().toString())){
//                    newMap.put(j.getAuthenticName(), i.get("totalNum"));
//                }
//            }
//        }
//        //根据用户侯吉光的id获取数据存入到统计信息中
//        for(SysUserEntity j : ry){
//            if(j.getAuthenticName().equals("侯吉光") && total == 0.0){
//                params.put("userId",j.getUserId());
//                params.put("outType","146");
//                total = businessOutlayDao.getTotalByHJGId(params);
//                //System.out.println(total);
//            }
//        }
//        newMap.put("侯吉光",total);
//        Map<String, Object> page = new HashMap<>();
//        page.put("lb",list);
//        page.put("tj",newMap);
//        return page;

        //根据条件获取类别统计信息
        List<Map<String,Object>> list = businessOutlayDao.getLbTotal(params);
        Map<String, Object> page = new HashMap<>();
        page.put("lb",list);
        return page;
    }

    @Override
    public void ifSave(Date outinDate, 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(outinDate).substring(0,4);
        // 时间比较 -1  前面的时间比后面的小   0 则前面的大 1则为相等
//        if(outinDate.compareTo(ft.parse(ifTime)) < 0){ //   说明传入的时间比每年的3月1号小 如果有当前年份则不需要添加
            if(!(businessAnnualDao.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())));
                businessAnnualDao.newSave(businessAnnualEntity);
            }
//        } else { // 大或者相等  第二年  如果没有则添加年份
            /*f(!(businessAnnualDao.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())));
                businessAnnualDao.newSave(businessAnnualEntity);
            }
        }*/
    }

    /**
     * 获取当前年度数据
     * @return
     */
    @Override
    public List<BusinessOutlayIncomeEntity> listNowTime(Map<String, Object> params) {
        return baseMapper.listNowTime(params);
    }

    /**
     * 获取昨日余额
     *
     * @param params
     * @return
     */
    @Override
    public BigDecimal getZrye(Map<String, Object> params) {
        return baseMapper.getZrye(params);
    }

    /**
     * 锁住
     *
     * @param userId
     */
    @Override
    public void updateLocked(String userId) {
        baseMapper.updateLocked(userId);
    }

    /**
     * 获取所选日期是否已经阶段提交
     *
     * @param params
     * @return
     */
    @Override
    public int getLockedCnt(Map<String, Object> params) {
        return baseMapper.getLockedCnt(params);
    }

    /**
     * 根据时间和执行人获取收支数据
     * @param params
     * @return
     */
    @Override
    public BusinessOutlayIncomeEntity getOutInByDate(Map<String, Object> params){
        return baseMapper.getOutInByDate(params);
    }

    /**
     * 根据时间和执行人获取下一日期收支数据
     * @param params
     * @return
     */
    @Override
    public BusinessOutlayIncomeEntity getNextOutInByDate(Map<String, Object> params){
        return baseMapper.getNextOutInByDate(params);
    }

}