package jnpf.workflow.service.impl;

import jnpf.base.service.SuperServiceImpl;
import jnpf.entity.workflow.PunishmentsApproval;
import jnpf.entity.workflow.RewardApproval;
import jnpf.model.workflow.dto.PunishmentsApprovalDto;
import jnpf.model.workflow.dto.UserSelfDto;
import jnpf.model.workflow.vo.PunishmentsApprovalVo;
import jnpf.model.workflow.vo.RewardApprovalVo;
import jnpf.util.JsonUtil;
import jnpf.util.StringUtil;
import jnpf.workflow.mapper.PunishmentsApprovalMapper;
import jnpf.workflow.service.PunishmentsApprovalService;
import org.springframework.stereotype.Service;
import org.yaml.snakeyaml.events.Event;

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

/**
 * @Author huanglinpan
 * @Date 2024/5/24 10:43
 * @Version 1.0 （版本号）
 */
@Service
public class PunishmentsApprovalServiceImpl extends SuperServiceImpl<PunishmentsApprovalMapper, PunishmentsApproval> implements PunishmentsApprovalService {

    @Resource
    private PunishmentsApprovalMapper punishmentsApprovalMapper;


    @Override
    public void punishmentsApprove(String applyId, Integer status) {
        PunishmentsApproval punishmentsApproval = this.getById(applyId);
        punishmentsApproval.setStatus(status);
        if (1 == status){
            punishmentsApproval.setEffectiveDate(new Date());
        }
        updateById(punishmentsApproval);
    }

    @Override
    public void savePunishments(String id, PunishmentsApproval entity, PunishmentsApprovalDto dto) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(id);
            save(entity);
            // 处理用户
            punishmentsApprovalMapper.saveUser(id,dto.getUserDtos());
        } else {
            entity.setId(id);
            updateById(entity);
            punishmentsApprovalMapper.deleteUserById(id);
            // 处理用户
            punishmentsApprovalMapper.saveUser(entity.getId(),dto.getUserDtos());
        }
    }

    @Override
    public void submitPunishments(String id, PunishmentsApproval entity, PunishmentsApprovalDto dto) {
        if (StringUtil.isEmpty(entity.getId())) {
            entity.setId(id);
            save(entity);
            // 处理用户
            punishmentsApprovalMapper.saveUser(id,dto.getUserDtos());
        } else {
            entity.setId(id);
            updateById(entity);
            punishmentsApprovalMapper.deleteUserById(id);
            // 处理用户
            punishmentsApprovalMapper.saveUser(entity.getId(),dto.getUserDtos());
        }
    }

    @Override
    public PunishmentsApprovalVo getPunishments(String id) {
        PunishmentsApproval punishmentsApproval = this.getById(id);
        PunishmentsApprovalVo jsonToBean = JsonUtil.getJsonToBean(punishmentsApproval, PunishmentsApprovalVo.class);
        // 拼接用户
        if (null != jsonToBean){
            List<UserSelfDto> userSelfDtos = punishmentsApprovalMapper.getListById(id);
            jsonToBean.setUserDtos(userSelfDtos);
        }
        return jsonToBean;
    }
}
