package com.sooka.apply.service.impl;

import java.util.Arrays;
import java.util.List;

import com.sooka.apply.domain.IntBo;
import com.sooka.common.core.text.Convert;
import com.sooka.common.utils.DateUtils;
import com.sooka.common.utils.StringUtils;
import com.sooka.common.utils.uuid.IdUtils;
import com.sooka.common.utils.uuid.UUID;
import com.sooka.framework.util.ShiroUtils;
import com.sooka.system.domain.SysUser;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import org.springframework.transaction.annotation.Transactional;
import com.sooka.apply.domain.IntDetailed;
import com.sooka.apply.mapper.IntRecordMapper;
import com.sooka.apply.domain.IntRecord;
import com.sooka.apply.service.IIntRecordService;

/**
 * 申请记录Service业务层处理
 * 
 * @author yhliang
 * @date 2021-07-06
 */
@Service
public class IntRecordServiceImpl implements IIntRecordService 
{
    @Autowired
    private IntRecordMapper intRecordMapper;

    /**
     * 查询申请记录
     * 
     * @param id 申请记录ID
     * @return 申请记录
     */
    @Override
    public IntRecord selectIntRecordById(String id)
    {
        return intRecordMapper.selectIntRecordById(id);
    }

    /**
     * 查询申请记录列表
     * 
     * @param intRecord 申请记录
     * @return 申请记录
     */
    @Override
    public List<IntRecord> selectIntRecordList(IntRecord intRecord)
    {
        return intRecordMapper.selectIntRecordList(intRecord);
    }

    /**
     * 查询申请记录明细
     *
     * @param intDetailed 查询条件
     * @return 申请明细集合
     */
    @Override
    public List<IntDetailed> selectIntDetailedList(IntDetailed intDetailed) {
        return intRecordMapper.selectIntDetailedList(intDetailed);
    }

    /**
     * 新增申请记录
     * 
     * @param intBo 申请记录
     * @return 结果
     */
    @Transactional
    @Override
    public int insertIntRecord(IntBo intBo)
    {

        SysUser user = ShiroUtils.getSysUser();

        IntRecord intRecord = new IntRecord();
        intRecord.setId(IdUtils.fastSimpleUUID());
        intRecord.setApplyOid(intBo.getDeptId());
        intRecord.setOrgId(user.getDeptId());
        intRecord.setApplyTime(DateUtils.getNowDate());
        intRecord.setIntNumbers(intBo.getIntIds().split(",").length);

        intBo.setId(intRecord.getId());

        try {
            insertIntDetailed(intBo);
            return intRecordMapper.insertIntRecord(intRecord);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;
    }

    /**
     * 删除申请记录对象
     *
     * @param ids 需要删除的数据ID
     * @return 结果
     */
    @Transactional
    @Override
    public int deleteIntRecordByIds(String ids)
    {
        intRecordMapper.deleteIntDetailedByRecordIds(Convert.toStrArray(ids));
        return intRecordMapper.deleteIntRecordByIds(Convert.toStrArray(ids));
    }

    /**
     * 状态修改
     *
     * @param intDetailed 接口信息
     * @return 结果
     */
    @Override
    public int changeStatus(IntDetailed intDetailed) {
        System.out.println(intDetailed.toString());
        return intRecordMapper.changeStatus(intDetailed);
    }

    /**
     * 新增申请明细信息
     * 
     * @param intBo 申请记录对象
     */
    private void insertIntDetailed(IntBo intBo) throws Exception {

        String[] intIds = intBo.getIntIds().split(",");
        String[] intCodes = intBo.getIntCodes().split(",");

        if (intIds.length != intCodes.length) {
            throw new Exception("接口信息不全，无法完成申请");
        }

        String id = intBo.getId();
        String str_at = intBo.getDatetime().split(" - ")[0];
        String end_at = intBo.getDatetime().split(" - ")[1];

        List<IntDetailed> list = new ArrayList<IntDetailed>();

        for (int i = 0; i < intIds.length; i++) {
            String intId = intIds[i];
            String intCode = intCodes[i];
            IntDetailed intDetailed = new IntDetailed();
            intDetailed.setId(IdUtils.fastSimpleUUID());
            intDetailed.setIntId(intId);
            intDetailed.setIntCode(intCode);
            intDetailed.setRecordId(id);
            intDetailed.setStrAt(str_at);
            intDetailed.setEndAt(end_at);
            intDetailed.setSecretKey(IdUtils.fastSimpleUUID());
            intDetailed.setStatus("1");
            list.add(intDetailed);
        }
        
        if (list.size() > 0) {
            intRecordMapper.batchIntDetailed(list);
        }
    }
}
