package jnpf.personnels.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import jnpf.base.entity.SuperBaseEntity;
import jnpf.model.personnels.dto.scheme.FtbPersonAddSchemeDTO;
import jnpf.model.personnels.dto.scheme.FtbPersonEditSchemeDTO;
import jnpf.model.personnels.po.FtbPersonnelsStaffRosterScheme;
import jnpf.model.personnels.vo.scheme.FtbPersonSchemeDetailsVO;
import jnpf.model.personnels.vo.scheme.FtbPersonSchemeListVO;
import jnpf.personnels.mapper.FtbPersonnelsStaffRosterSchemeMapper;
import jnpf.personnels.service.FtbPersonnelsStaffRosterSchemeService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class FtbPersonnelsStaffRosterSchemeServiceImpl implements FtbPersonnelsStaffRosterSchemeService {

    @Resource
    private FtbPersonnelsStaffRosterSchemeMapper ftbPersonnelsStaffRosterSchemeMapper;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void addScheme(FtbPersonAddSchemeDTO schemeDTO) {
        // 方案上限十个，每个方案字数上限10字
        LambdaQueryWrapper<FtbPersonnelsStaffRosterScheme> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffRosterScheme::getEnableMark, 0);
        if (ftbPersonnelsStaffRosterSchemeMapper.selectCount(queryWrapper) > 10) {
            throw new RuntimeException("方案数量已达上限10个");
        }
        // 方案名称不能重复
        queryWrapper.eq(FtbPersonnelsStaffRosterScheme::getSchemeName, schemeDTO.getSchemeName());
        if (ftbPersonnelsStaffRosterSchemeMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("方案名称不能重复");
        }
        FtbPersonnelsStaffRosterScheme ftbPersonnelsStaffRosterScheme = schemeDTO.convert(schemeDTO);
        ftbPersonnelsStaffRosterSchemeMapper.insert(ftbPersonnelsStaffRosterScheme);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteScheme(String id) {
        LambdaUpdateWrapper<FtbPersonnelsStaffRosterScheme> deleteWrapper = Wrappers.lambdaUpdate();
        deleteWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, id);
        deleteWrapper.set(FtbPersonnelsStaffRosterScheme::getEnableMark,1);
        ftbPersonnelsStaffRosterSchemeMapper.update(new FtbPersonnelsStaffRosterScheme(), deleteWrapper);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateScheme(FtbPersonEditSchemeDTO schemeDTO) {
        // 方案名称不能重复
        LambdaQueryWrapper<FtbPersonnelsStaffRosterScheme> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffRosterScheme::getSchemeName, schemeDTO.getSchemeName());
        queryWrapper.ne(SuperBaseEntity.SuperIBaseEntity::getId, schemeDTO.getSchemeId());
        queryWrapper.eq(FtbPersonnelsStaffRosterScheme::getEnableMark, 0);
        if (ftbPersonnelsStaffRosterSchemeMapper.selectCount(queryWrapper) > 0) {
            throw new RuntimeException("方案名称不能重复");
        }
        LambdaUpdateWrapper<FtbPersonnelsStaffRosterScheme> updateWrapper = Wrappers.lambdaUpdate();
        updateWrapper.eq(SuperBaseEntity.SuperIBaseEntity::getId, schemeDTO.getSchemeId());
        updateWrapper.set(FtbPersonnelsStaffRosterScheme::getSchemeName, schemeDTO.getSchemeName());
        updateWrapper.set(FtbPersonnelsStaffRosterScheme::getFormFields, JSON.toJSONString(schemeDTO.getFormFields()));
        ftbPersonnelsStaffRosterSchemeMapper.update(new FtbPersonnelsStaffRosterScheme(), updateWrapper);
    }

    @Override
    public FtbPersonSchemeDetailsVO detailScheme(String id) {
        FtbPersonnelsStaffRosterScheme ftbPersonnelsStaffRosterScheme = ftbPersonnelsStaffRosterSchemeMapper.selectById(id);
        FtbPersonSchemeDetailsVO ftbPersonSchemeDetailsVO = new FtbPersonSchemeDetailsVO();
        ftbPersonSchemeDetailsVO.setSchemeId(ftbPersonnelsStaffRosterScheme.getId());
        ftbPersonSchemeDetailsVO.setSchemeName(ftbPersonnelsStaffRosterScheme.getSchemeName());
        ftbPersonSchemeDetailsVO.setFormFields(JSON.parseArray(ftbPersonnelsStaffRosterScheme.getFormFields(), String.class));
        return ftbPersonSchemeDetailsVO;
    }

    @Override
    public List<FtbPersonSchemeListVO> getSchemeList() {
        LambdaQueryWrapper<FtbPersonnelsStaffRosterScheme> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(FtbPersonnelsStaffRosterScheme::getEnableMark, 0);
        queryWrapper.orderByDesc(FtbPersonnelsStaffRosterScheme::getCreatorTime);
        return ftbPersonnelsStaffRosterSchemeMapper.selectList(queryWrapper).stream().map(item -> {
            FtbPersonSchemeListVO ftbPersonSchemeListVO = new FtbPersonSchemeListVO();
            ftbPersonSchemeListVO.setSchemeId(item.getId());
            ftbPersonSchemeListVO.setSchemeName(item.getSchemeName());
            return ftbPersonSchemeListVO;
        }).collect(Collectors.toList());

    }

}
