package jnpf.workflow.service.impl;

import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.workflow.AttendanceLeaveApprove;
import jnpf.entity.workflow.RewardApproval;
import jnpf.model.workflow.dto.RewardApprovalDto;
import jnpf.model.workflow.dto.UserSelfDto;
import jnpf.model.workflow.vo.AttendanceLeaveFlowApproveVo;
import jnpf.model.workflow.vo.RewardApprovalVo;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.workflow.mapper.AttendanceLeaveMapper;
import jnpf.workflow.mapper.RewardApprovalMapper;
import jnpf.workflow.service.LeaveApproveService;
import jnpf.workflow.service.RewardService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Author huanglinpan
 * @Date 2024/5/21 17:25
 * @Version 1.0 （版本号）
 */
@Service
public class RewardServiceImpl extends SuperServiceImpl<RewardApprovalMapper, RewardApproval> implements RewardService {

    @Resource
    private RewardApprovalMapper rewardApprovalMapper;


    @Override
    public void saveReward(String id, RewardApproval entity, RewardApprovalDto dto) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(id);
            save(entity);
            // 处理用户
            rewardApprovalMapper.saveUser(id,dto.getUserDtos());
        } else {
            entity.setId(id);
            updateById(entity);
            rewardApprovalMapper.deleteUserById(id);
            // 处理用户
            rewardApprovalMapper.saveUser(entity.getId(),dto.getUserDtos());
        }
    }

    @Override
    public void submitReward(String id, RewardApproval entity, RewardApprovalDto dto) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(id);
            save(entity);
            // 处理用户
            rewardApprovalMapper.saveUser(id,dto.getUserDtos());
        } else {
            entity.setId(id);
            updateById(entity);
            rewardApprovalMapper.deleteUserById(id);
            // 处理用户
            rewardApprovalMapper.saveUser(entity.getId(),dto.getUserDtos());
        }
    }

    @Override
    public RewardApprovalVo getReward(String id) {
        RewardApproval rewardApproval = this.getById(id);
        RewardApprovalVo jsonToBean = JsonUtil.getJsonToBean(rewardApproval, RewardApprovalVo.class);
        // 拼接用户
        if (null != jsonToBean){
            List<UserSelfDto> userSelfDtos = rewardApprovalMapper.getListById(id);
            jsonToBean.setUserDtos(userSelfDtos);
        }
        return jsonToBean;
    }

    @Override
    public void rewardApprove(String id, Integer status) {
        RewardApproval rewardApproval = this.getById(id);
        rewardApproval.setStatus(status);
        if (1 == status) {
            rewardApproval.setEffectiveDate(new Date());
        }
        //修改状态
        updateById(rewardApproval);
    }
}
