package com.zhongzheng.modules.user.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import com.zhongzheng.common.utils.DateUtils;
import com.zhongzheng.modules.user.bo.UserBankQuestionRecordAddBo;
import com.zhongzheng.modules.user.bo.UserBankRecordAddBo;
import com.zhongzheng.modules.user.bo.UserBankRecordEditBo;
import com.zhongzheng.modules.user.bo.UserBankRecordQueryBo;
import com.zhongzheng.modules.user.domain.User;
import com.zhongzheng.modules.user.domain.UserBankRecord;
import com.zhongzheng.modules.user.mapper.UserBankRecordMapper;
import com.zhongzheng.modules.user.service.IUserBankQuestionRecordService;
import com.zhongzheng.modules.user.service.IUserBankRecordService;
import com.zhongzheng.modules.user.service.IUserService;
import com.zhongzheng.modules.user.vo.UserBankRecordVo;
import com.zhongzheng.modules.user.vo.UserVo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.github.pagehelper.Page;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * 用户的做题历史Service业务层处理
 *
 * @author ruoyi
 * @date 2021-06-24
 */
@Service
public class UserBankRecordServiceImpl extends ServiceImpl<UserBankRecordMapper, UserBankRecord> implements IUserBankRecordService {

    @Autowired
    private IUserBankQuestionRecordService iUserBankQuestionRecordService;


    @Autowired
    private IUserService userService;



    private static Logger log = LoggerFactory.getLogger(UserBankRecordServiceImpl.class);


    @Override
    public UserBankRecordVo queryById(Long recordId){
        UserBankRecord db = this.baseMapper.selectById(recordId);
        return BeanUtil.toBean(db, UserBankRecordVo.class);
    }

    @Override
    public List<UserBankRecordVo> queryList(UserBankRecordQueryBo bo) {
        LambdaQueryWrapper<UserBankRecord> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getType() != null, UserBankRecord::getType, bo.getType());
        lqw.eq(bo.getSimulateId() != null, UserBankRecord::getSimulateId, bo.getSimulateId());
        lqw.eq(bo.getExamId() != null, UserBankRecord::getExamId, bo.getExamId());
        lqw.eq(bo.getBankId() != null, UserBankRecord::getBankId, bo.getBankId());
        return entity2Vo(this.list(lqw));
    }

    /**
    * 实体类转化成视图对象
    *
    * @param collection 实体类集合
    * @return
    */
    private List<UserBankRecordVo> entity2Vo(Collection<UserBankRecord> collection) {
        List<UserBankRecordVo> voList = collection.stream()
                .map(any -> BeanUtil.toBean(any, UserBankRecordVo.class))
                .collect(Collectors.toList());
        if (collection instanceof Page) {
            Page<UserBankRecord> page = (Page<UserBankRecord>)collection;
            Page<UserBankRecordVo> pageVo = new Page<>();
            BeanUtil.copyProperties(page,pageVo);
            pageVo.addAll(voList);
            voList = pageVo;
        }
        return voList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean insertByAddBo(UserBankRecordAddBo bo) {
        UserBankRecord add = BeanUtil.toBean(bo, UserBankRecord.class);
        validEntityBeforeSave(add);
        add.setCreateTime(DateUtils.getNowTime());
        add.setUpdateTime(DateUtils.getNowTime());

        //模拟考试获得用户积分
        if(bo.getType() != null && bo.getType() == 1){

        }

        if(this.save(add)){
            Long recordId =  add.getRecordId();
            for(UserBankQuestionRecordAddBo item : bo.getAnswerList()){
                item.setBankId(bo.getBankId());
                item.setRecordId(recordId);
                item.setUserId(bo.getUserId());
                iUserBankQuestionRecordService.insertByAddBo(item);
            }
            return true;
        }
        return false;

    }



    private Boolean increaseUser(UserVo userVo,Long score){
        User user = BeanUtil.toBean(userVo, User.class);
        user.setIntegral(score);
        user.setUpdateTime(DateUtils.getNowTime());
        return userService.updateById(user);
    }

    @Override
    public Boolean updateByEditBo(UserBankRecordEditBo bo) {
        UserBankRecord update = BeanUtil.toBean(bo, UserBankRecord.class);
        validEntityBeforeSave(update);
        update.setUpdateTime(DateUtils.getNowTime());
        return this.updateById(update);
    }

    /**
     * 保存前的数据校验
     *
     * @param entity 实体类数据
     */
    private void validEntityBeforeSave(UserBankRecord entity){
        //TODO 做一些数据校验,如唯一约束
    }

    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return this.removeByIds(ids);
    }


    //获得今天时间的0点
    public static int getTimesmorning(){
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, 0);

        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.MINUTE, 0);

        cal.set(Calendar.MILLISECOND, 0);

        return (int) (cal.getTimeInMillis()/1000);

    }

    //获得当天24点时间
    public static int getTimesnight(){
        Calendar cal = Calendar.getInstance();

        cal.set(Calendar.HOUR_OF_DAY, 24);

        cal.set(Calendar.SECOND, 0);

        cal.set(Calendar.MINUTE, 0);

        cal.set(Calendar.MILLISECOND, 0);

        return (int) (cal.getTimeInMillis()/1000);

    }
}
