package com.zd.exam.service.impl;

import com.zd.common.core.domain.R;
import com.zd.common.core.utils.DateUtils;
import com.zd.common.core.utils.SecurityUtils;
import com.zd.common.security.utils.SaveUtil;
import com.zd.exam.domain.ElScoreRewardconfig;
import com.zd.exam.mapper.ElScoreRewardconfigMapper;
import com.zd.exam.service.IElPointsRecordService;
import com.zd.exam.service.IElScoreRewardconfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 奖励分设置Service业务层处理
 *
 * @author zd
 * @date 2021-12-14
 */
@Service
public class ElScoreRewardconfigServiceImpl implements IElScoreRewardconfigService
{
    @Autowired
    private ElScoreRewardconfigMapper elScoreRewardconfigMapper;

    @Autowired
    IElPointsRecordService recordService;

    /**
     * 查询奖励分设置
     *
     * @param id 奖励分设置主键
     * @return 奖励分设置
     */
    @Override
    public ElScoreRewardconfig selectElScoreRewardconfigById(Long id)
    {
        return elScoreRewardconfigMapper.selectElScoreRewardconfigById(id);
    }

    /**
     * 查询奖励分设置列表
     *
     * @param elScoreRewardconfig 奖励分设置
     * @return 奖励分设置
     */
    @Override
    public List<ElScoreRewardconfig> selectElScoreRewardconfigList(ElScoreRewardconfig elScoreRewardconfig)
    {
        return elScoreRewardconfigMapper.selectElScoreRewardconfigList(elScoreRewardconfig);
    }

    /**
     * 新增奖励分设置
     *
     * @param elScoreRewardconfig 奖励分设置
     * @return 结果
     */
    @Override
    public int insertElScoreRewardconfig(ElScoreRewardconfig elScoreRewardconfig)
    {
        //设置其他公共字段
//        SaveUtil.setCommonAttr(elScoreRewardconfig);
        return elScoreRewardconfigMapper.insertElScoreRewardconfig(elScoreRewardconfig);
    }

    /**
     * 修改奖励分设置
     *
     * @param elScoreRewardconfig 奖励分设置
     * @return 结果
     */
    @Override
    public int updateElScoreRewardconfig(ElScoreRewardconfig elScoreRewardconfig)
    {
        elScoreRewardconfig.setUpdateTime(DateUtils.getNowDate());
        elScoreRewardconfig.setUpdateBy(SecurityUtils.getUsername());
        return elScoreRewardconfigMapper.updateElScoreRewardconfig(elScoreRewardconfig);
    }

    /**
     * 批量删除奖励分设置
     *
     * @param ids 需要删除的奖励分设置主键
     * @return 结果
     */
    @Override
    public int deleteElScoreRewardconfigByIds(Long[] ids)
    {
        return elScoreRewardconfigMapper.deleteElScoreRewardconfigByIds(ids);
    }

    /**
     * 删除奖励分设置信息
     *
     * @param id 奖励分设置主键
     * @return 结果
     */
    @Override
    public int deleteElScoreRewardconfigById(Long id)
    {
        return elScoreRewardconfigMapper.deleteElScoreRewardconfigById(id);
    }


    @Override
    @Transactional
    public R addLoginfo(Map map) {
        List<ElScoreRewardconfig> list = this.selectElScoreRewardconfigList(new ElScoreRewardconfig());
        if (list.size() > 0 && list!=null){
            ElScoreRewardconfig rewardconfig = list.get(0);
            Map<String,Object> param = new HashMap<>();
            param.put("joinUserId",map.get("userId"));

            //进入
            if (map.get("status").equals("0")){
                param.put("deductPoints",rewardconfig.getSignGetScore());
                param.put("reason","完成签到考勤获得奖励分值");
            }else{//离开
                param.put("deductPoints",rewardconfig.getLeaveGetScore());
                param.put("reason","完成离开考勤获得奖励分值");
            }
            int row =  recordService.obtainBonusPoints(param);
            if (row > 0)return R.ok();
            return R.fail(-1,"保存失败");
        }

        return R.fail(-2,"没有找到奖励分规则,请先添加");
    }
}
