package com.willalp.consume.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.consume.domain.ConsumeMaintain;
import com.willalp.consume.mapper.ConsumeMaintainMapper;
import com.willalp.consume.service.IConsumeMaintainService;
import com.willalp.system.mapper.SysUserOrganizationMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * 订餐消费维护Service业务层处理
 *
 * @author willalp
 * @date 2021-12-02
 */
@Service
public class ConsumeMaintainServiceImpl extends ServiceImpl<ConsumeMaintainMapper, ConsumeMaintain> implements IConsumeMaintainService {
    @Autowired
    private ConsumeMaintainMapper consumeMaintainMapper;

    @Autowired
    private SysUserOrganizationMapper sysUserOrganizationMapper;

    /**
     * 查询订餐消费维护列表
     *
     * @param consumeMaintain 订餐消费维护
     * @return 订餐消费维护
     */
    @Override
    public List<ConsumeMaintain> selectConsumeMaintainList(ConsumeMaintain consumeMaintain) {
        List<ConsumeMaintain> consumeMaintains = consumeMaintainMapper.selectConsumeMaintainList(consumeMaintain);
        Date date = new Date();
        consumeMaintains.forEach(o -> {
            if (ObjectUtil.isNotNull(o.getSendTime())) {
                Date begin = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", DateUtils.parseDateToStr("yyyy-MM-dd", o.getSendTime()) + " " + o.getBeginDate() + ":00");
                Date end = DateUtils.dateTime("yyyy-MM-dd HH:mm:ss", DateUtils.parseDateToStr("yyyy-MM-dd", o.getSendTime()) + " " + o.getEndDate() + ":00");
                if (date.compareTo(begin) < 0) {
                    o.setSendStatus(0);
                } else if (date.compareTo(end) > 0) {
                    o.setSendStatus(2);
                } else {
                    o.setSendStatus(1);
                }
            }
        });
        return consumeMaintains;
    }

    @Override
    public void saveInfo(ConsumeMaintain consumeMaintain) {
//        SysUserOrganization org = new SysUserOrganization();
//        org.setLoginName(SecurityUtils.getUsername());
//        List<SysUserOrganization> list = sysUserOrganizationMapper.selectSysUserOrganizationList(org);
//        if (!list.isEmpty()) {
//            list = list.stream().filter(e -> e.getIsDefault() != null && e.getIsDefault() == 1).collect(Collectors.toList());
//            if(!list.isEmpty()) {
//                consumeMaintain.setOrganizationCode(list.get(0).getOrganizationCode());
//            }
//        }

        while (true) {
            String idStr = IdWorker.getIdStr();
            ConsumeMaintain byId = consumeMaintainMapper.selectById(idStr);
            if (null == byId) {
                consumeMaintain.setId(idStr);
                break;
            }
        }
        consumeMaintain.setIsDelete(0);
        consumeMaintain.setCreateBy(SecurityUtils.getUsername());
        consumeMaintain.setCreateTime(DateUtils.getNowDate());
        consumeMaintainMapper.insert(consumeMaintain);
    }


}
