package com.usky.fire.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.usky.common.core.bean.CommonPage;
import com.usky.common.core.exception.BusinessException;
import com.usky.common.mybatis.core.AbstractCrudService;
import com.usky.common.security.utils.SecurityUtils;
import com.usky.fire.domain.ReservePlan;
import com.usky.fire.mapper.ReservePlanMapper;
import com.usky.fire.service.ReservePlanService;
import com.usky.fire.service.vo.DataCountVo;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 预案管理 服务实现类
 * </p>
 *
 * @author JCB
 * @since 2022-09-05
 */
@Service
public class ReservePlanServiceImpl extends AbstractCrudService<ReservePlanMapper, ReservePlan> implements ReservePlanService {

    @Override
    public void reservePlanDataAdd(ReservePlan reservePlan) {
        LambdaQueryWrapper<ReservePlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReservePlan::getReserveName, reservePlan.getReserveName())
                .eq(ReservePlan::getCreator, SecurityUtils.getUsername())
                .eq(ReservePlan::getEnable, 1);
        List<ReservePlan> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            throw new BusinessException("预案民称重复不可添加");
        }
        reservePlan.setEnable(1);
        reservePlan.setCreateTime(LocalDateTime.now());
        reservePlan.setCreator(SecurityUtils.getUsername());
        this.save(reservePlan);
    }

    @Override
    public void reservePlanDataUpdate(ReservePlan reservePlan) {
        LambdaQueryWrapper<ReservePlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(ReservePlan::getReserveName, reservePlan.getReserveName())
                .eq(ReservePlan::getCreator, SecurityUtils.getUsername())
                .ne(ReservePlan::getId, reservePlan.getId())
                .eq(ReservePlan::getEnable, 1);
        List<ReservePlan> list = this.list(queryWrapper);
        if (CollectionUtils.isNotEmpty(list)) {
            throw new BusinessException("预案民称重复不可添加");
        }
        reservePlan.setUpdatePerson(SecurityUtils.getUsername());
        reservePlan.setUpdateTime(LocalDateTime.now());
        this.updateById(reservePlan);
    }

    @Override
    public void reservePlanDataDelete(Integer id) {
        ReservePlan reservePlan = new ReservePlan();
        reservePlan.setId(id);
        reservePlan.setEnable(3);
        reservePlan.setUpdatePerson(SecurityUtils.getUsername());
        reservePlan.setUpdateTime(LocalDateTime.now());
        this.updateById(reservePlan);
    }

    @Override
    public CommonPage<Object> reservePlanDataList(String reserveName, String startDate, String endDate, Integer pageNum, Integer pageSize) {
        List<Object> list = new ArrayList<>();
        IPage<ReservePlan> page = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<ReservePlan> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.select(ReservePlan::getId, ReservePlan::getReserveName, ReservePlan::getReserveUrl, ReservePlan::getReserveType,
                ReservePlan::getCreator, ReservePlan::getCreateTime, ReservePlan::getCompanyId)
                .eq(ReservePlan::getEnable, 1)
                .eq(ReservePlan::getCreator, SecurityUtils.getUsername())
                .like(StringUtils.isNotBlank(reserveName), ReservePlan::getReserveName, reserveName)
                .between(StringUtils.isNotBlank(startDate) && StringUtils.isNotBlank(endDate), ReservePlan::getCreateTime, startDate, endDate)
                .orderByDesc(ReservePlan::getId);
        page = this.page(page, queryWrapper);
        if (CollectionUtils.isNotEmpty(page.getRecords())) {
            for (int i = 0; i < page.getRecords().size(); i++) {
                Map<String, Object> map = new HashMap<>();
                map.put("id", page.getRecords().get(i).getId());
                map.put("reserveName", page.getRecords().get(i).getReserveName());
                map.put("reserveType", page.getRecords().get(i).getReserveType());
                map.put("reserveUrl", page.getRecords().get(i).getReserveUrl());
                map.put("creator", page.getRecords().get(i).getCreator());
                map.put("createTime", page.getRecords().get(i).getCreateTime());
                map.put("companyId", page.getRecords().get(i).getCompanyId());
                list.add(map);
            }
        }
        return new CommonPage<>(list, page.getTotal(), pageSize, pageNum);
    }

    @Override
    public List<DataCountVo> reservePlanCount(String companyId) {
        List<DataCountVo> dataCountVos = baseMapper.reservePlanCount(companyId);
        return dataCountVos;
    }

}
