package com.ruoyi.syzy.service.impl;

import com.alibaba.fastjson2.JSON;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.utils.sign.Md5Utils;
import com.ruoyi.syzy.domain.BBusiWishRecords;
import com.ruoyi.syzy.dto.SubmitWishRecordDTO;
import com.ruoyi.syzy.mapper.BBusiWishRecordsMapper;
import com.ruoyi.syzy.service.IBBusiWishRecordsService;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 志愿记录Service业务层处理
 * 
 * @author mingxue
 * @date 2021-08-23
 */
@Service
public class BBusiWishRecordsServiceImpl implements IBBusiWishRecordsService {
    @Autowired
    private BBusiWishRecordsMapper bBusiWishRecordsMapper;

    /**
     * 查询志愿记录
     * 
     * @param id 志愿记录ID
     * @return 志愿记录
     */
    @Override
    public BBusiWishRecords selectBBusiWishRecordsById(Long id) {
        return bBusiWishRecordsMapper.selectBBusiWishRecordsById(id);
    }

    /**
     * 查询志愿记录列表
     * 
     * @param bBusiWishRecords 志愿记录
     * @return 志愿记录
     */
    @Override
    public List<BBusiWishRecords> selectBBusiWishRecordsList(BBusiWishRecords bBusiWishRecords) {
        return bBusiWishRecordsMapper.selectBBusiWishRecordsList(bBusiWishRecords);
    }

    private Integer getMaxWishIndex(String customerCode) {
        BBusiWishRecords cond = new BBusiWishRecords();
        cond.setCustomerCode(customerCode);
        cond.setStatus(1);
        List<BBusiWishRecords> existList = bBusiWishRecordsMapper.selectBBusiWishRecordsList(cond);
        Integer maxId = 1;
        for(BBusiWishRecords record : existList) {
            Integer id = NumberUtils.toInt(StringUtils.replace(record.getName(), "志愿表", ""), 0) + 1;
            if(id > maxId) {
                maxId = id;
            }
        }
        return maxId;
    }

    @Override
    public void save(SubmitWishRecordDTO data) {
        String customerCode = String.valueOf(SecurityUtils.getLoginUser().getUser().getUserId());
        BBusiWishRecords entity = new BBusiWishRecords();
        entity.setCustomerCode(customerCode);
        entity.setCode(Md5Utils.hash(System.currentTimeMillis() + customerCode));
        entity.setType(1);
        entity.setBatch(String.valueOf(data.getBatch()));
        entity.setUserSnapshot(JSON.toJSONString(data.getUserSnapshot()));
        entity.setDetail(JSON.toJSONString(data.getDetail()));
        entity.setStatus(1);
        entity.setBatchName(null==data.getDetail()?null:(null==data.getDetail().getBatch()?null:data.getDetail().getBatch().getName()));
        entity.setName(data.getName());
        if (StringUtils.isNotBlank(data.getName())) {
            BBusiWishRecords cond = new BBusiWishRecords();
            cond.setCustomerCode(customerCode);
            cond.setStatus(1);
            cond.setName(data.getName());
            cond.setExactName(true);
            List<BBusiWishRecords> existList = bBusiWishRecordsMapper.selectBBusiWishRecordsList(cond);
            if (CollectionUtils.isNotEmpty(existList) && (null == data.getId() || 0L == data.getId() || !data.getId().equals(existList.get(0).getId()))) {
                throw new RuntimeException("志愿名称已经存在：" + data.getName());
            }
        }
        if (null != data.getId() && data.getId() > 0) {
            BBusiWishRecords cond = new BBusiWishRecords();
            cond.setCustomerCode(customerCode);
            cond.setId(data.getId());
            List<BBusiWishRecords> existList = bBusiWishRecordsMapper.selectBBusiWishRecordsList(cond);
            if (existList.size() != 1) {
                throw new RuntimeException("志愿id不存在");
            }
            BBusiWishRecords wr = existList.get(0);
            if(wr.getObsoleted()) {
                throw new RuntimeException("作废志愿不可修改: " + wr.getId());
            }
            entity.setId(wr.getId());
            if (StringUtils.isBlank(entity.getName())) {
                entity.setName(wr.getName());
            }
            updateBBusiWishRecords(entity);
        } else {
            if (StringUtils.isBlank(entity.getName())) {
                entity.setName("志愿表" + getMaxWishIndex(customerCode));
            }
            entity.setObsoleted(false);
            insertBBusiWishRecords(entity);
            data.setId(entity.getId());
        }
    }

    /**
     * 新增志愿记录
     * 
     * @param bBusiWishRecords 志愿记录
     * @return 结果
     */
    @Override
    public int insertBBusiWishRecords(BBusiWishRecords bBusiWishRecords) {
        bBusiWishRecords.setCreateTime(DateUtils.getNowDate());
        return bBusiWishRecordsMapper.insertBBusiWishRecords(bBusiWishRecords);
    }

    /**
     * 修改志愿记录
     * 
     * @param bBusiWishRecords 志愿记录
     * @return 结果
     */
    @Override
    public int updateBBusiWishRecords(BBusiWishRecords bBusiWishRecords) {
        return bBusiWishRecordsMapper.updateBBusiWishRecords(bBusiWishRecords);
    }

    /**
     * 批量删除志愿记录
     * 
     * @param ids 需要删除的志愿记录ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishRecordsByIds(Long[] ids) {
        return bBusiWishRecordsMapper.deleteBBusiWishRecordsByIds(ids);
    }

    /**
     * 删除志愿记录信息
     * 
     * @param id 志愿记录ID
     * @return 结果
     */
    @Override
    public int deleteBBusiWishRecordsById(Long id) {
        return bBusiWishRecordsMapper.deleteBBusiWishRecordsById(id);
    }
}
