package com.fastbee.iot.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.fastbee.common.core.domain.AjaxResult;
import com.fastbee.common.extend.core.domin.model.LoginUser;
import com.fastbee.common.extend.utils.SecurityUtils;
import com.fastbee.common.utils.DateUtils;
import com.fastbee.common.utils.MessageUtils;
import com.fastbee.common.utils.StringUtils;
import com.fastbee.iot.convert.OrderControlConvert;
import com.fastbee.iot.domain.OrderControl;
import com.fastbee.iot.mapper.OrderControlMapper;
import com.fastbee.iot.model.vo.OrderControlVO;
import com.fastbee.iot.model.vo.ThingsModelVO;
import com.fastbee.iot.service.IOrderControlService;
import com.fastbee.iot.service.IThingsModelService;
import org.springframework.stereotype.Service;

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

/**
 * 指令权限控制Service业务层处理
 *
 * @author kerwincui
 * @date 2024-07-01
 */
@Service
public class OrderControlServiceImpl extends ServiceImpl<OrderControlMapper,OrderControl> implements IOrderControlService
{
    @Resource
    private OrderControlMapper orderControlMapper;
    @Resource
    private IThingsModelService thingsModelService;

    /**
     * 查询指令权限控制
     *
     * @param id 指令权限控制主键
     * @return 指令权限控制
     */
    @Override
    public OrderControl selectOrderControlById(Long id)
    {
        return orderControlMapper.selectById(id);
    }

    /**
     * 查询指令权限控制列表
     *
     * @param orderControl 指令权限控制
     * @return 指令权限控制
     */
    @Override
    public List<OrderControlVO> selectOrderControlList(OrderControl orderControl)
    {
        orderControl.setTenantId(SecurityUtils.getDeptId());
        LambdaQueryWrapper<OrderControl> wrapper = this.buildQueryWrapper(orderControl);
        List<OrderControl> orderControls = orderControlMapper.selectList(wrapper);
        List<OrderControlVO> orderControlVOS = OrderControlConvert.INSTANCE.convertOrderControlVOList(orderControls);
        for (OrderControlVO control : orderControlVOS) {
            String[] split = control.getSelectOrder().split(",");
            List<Long> ids = Arrays.stream(split)
                    .map(s -> {
                        try {
                            return Long.parseLong(s.trim());
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            ThingsModelVO thingsModelVO = new ThingsModelVO();
            thingsModelVO.setModelIdList(ids);
            List<ThingsModelVO> thingsModels = thingsModelService.selectThingsModelList(thingsModelVO);
            String names = thingsModels.stream().map(ThingsModelVO::getModelName).collect(Collectors.joining(","));
            control.setModelNames(names);
            //判断是否在执行时间内的控制
            if (Objects.nonNull(control.getStartTime()) && Objects.nonNull(control.getEndTime())) {
                Date now = DateUtils.getNowDate();
                Date startTime = control.getStartTime();
                Date endTime = control.getEndTime();
                if (now.after(startTime) && now.before(endTime) && control.getCount() > 0) {
                    control.setStatus(1);
                }else {
                    control.setStatus(0);
                }
            }
        }
        return orderControlVOS;
    }

    /**
     * 查询指令权限控制分页列表
     *
     * @param orderControlVO 指令权限控制
     * @return 指令权限控制
     */
    @Override
    public Page<OrderControlVO> pageOrderControlVO(OrderControlVO orderControlVO) {
        orderControlVO.setTenantId(SecurityUtils.getDeptId());
        Page<OrderControlVO> orderControlVOPage = orderControlMapper.selectOrderControlVoPage(new Page<>(orderControlVO.getPageNum(), orderControlVO.getPageSize()), orderControlVO);
        if (0 == orderControlVOPage.getTotal()) {
            return new Page<>();
        }
        List<OrderControlVO> voList = orderControlVOPage.getRecords();
        for (OrderControlVO control : voList) {
            String[] split = control.getSelectOrder().split(",");
            List<Long> ids = Arrays.stream(split)
                    .map(s -> {
                        try {
                            return Long.parseLong(s.trim());
                        } catch (NumberFormatException e) {
                            return null;
                        }
                    })
                    .filter(Objects::nonNull)
                    .collect(Collectors.toList());
            ThingsModelVO thingsModelVO = new ThingsModelVO();
            thingsModelVO.setModelIdList(ids);
            List<ThingsModelVO> thingsModels = thingsModelService.selectThingsModelList(thingsModelVO);
            String names = thingsModels.stream().map(ThingsModelVO::getModelName).collect(Collectors.joining(","));
            control.setModelNames(names);
            //判断是否在执行时间内的控制
            if (Objects.nonNull(control.getStartTime()) && Objects.nonNull(control.getEndTime())) {
                Date now = DateUtils.getNowDate();
                Date startTime = control.getStartTime();
                Date endTime = control.getEndTime();
                if (now.after(startTime) && now.before(endTime) && control.getCount() > 0) {
                    control.setStatus(1);
                }else {
                    control.setStatus(0);
                }
            }
        }
        return orderControlVOPage;
    }

    private LambdaQueryWrapper<OrderControl> buildQueryWrapper(OrderControl query) {
        Map<String, Object> params = query.getParams();
        LambdaQueryWrapper<OrderControl> lqw = Wrappers.lambdaQuery();
        lqw.eq(query.getTenantId() != null, OrderControl::getTenantId, query.getTenantId());
        lqw.eq(StringUtils.isNotBlank(query.getSelectOrder()), OrderControl::getSelectOrder, query.getSelectOrder());
        lqw.eq(query.getStatus() != null, OrderControl::getStatus, query.getStatus());
        lqw.eq(query.getUserId() != null, OrderControl::getUserId, query.getUserId());
        lqw.eq(query.getDeviceId() != null, OrderControl::getDeviceId, query.getDeviceId());
        lqw.eq(query.getCount() != null, OrderControl::getCount, query.getCount());
        lqw.eq(query.getStartTime() != null, OrderControl::getStartTime, query.getStartTime());
        lqw.eq(query.getEndTime() != null, OrderControl::getEndTime, query.getEndTime());
        lqw.eq(StringUtils.isNotBlank(query.getFilePath()), OrderControl::getFilePath, query.getFilePath());
        lqw.eq(StringUtils.isNotBlank(query.getImgUrl()), OrderControl::getImgUrl, query.getImgUrl());

        if (!Objects.isNull(params.get("beginTime")) &&
                !Objects.isNull(params.get("endTime"))) {
            lqw.between(OrderControl::getCreateTime, params.get("beginTime"), params.get("endTime"));
        }
        return lqw;
    }

    /**
     * 新增指令权限控制
     *
     * @param orderControl 指令权限控制
     * @return 结果
     */
    @Override
    public int insertOrderControl(OrderControl orderControl)
    {
        orderControl.setTenantId(SecurityUtils.getDeptId());
        orderControl.setCreateTime(DateUtils.getNowDate());
        orderControl.setStatus(1);
        return orderControlMapper.insert(orderControl);
    }

    /**
     * 修改指令权限控制
     *
     * @param orderControl 指令权限控制
     * @return 结果
     */
    @Override
    public int updateOrderControl(OrderControl orderControl)
    {
        orderControl.setUpdateTime(DateUtils.getNowDate());
        return orderControlMapper.updateById(orderControl);
    }

    /**
     * 批量删除指令权限控制
     *
     * @param ids 需要删除的指令权限控制主键
     * @return 结果
     */
    @Override
    public int deleteOrderControlByIds(Long[] ids)
    {
        return orderControlMapper.deleteBatchIds(Arrays.asList(ids));
    }

    /**
     * 删除指令权限控制信息
     *
     * @param id 指令权限控制主键
     * @return 结果
     */
    @Override
    public int deleteOrderControlById(Long id)
    {
        return orderControlMapper.deleteById(id);
    }

    /**
     * 根据用户id获取
     * @param userId
     * @return
     */
    @Override
    public List<OrderControl> selectByUserId(Long userId,Long deviceId){
        LambdaQueryWrapper<OrderControl> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderControl::getUserId, userId);
        queryWrapper.eq(OrderControl::getDeviceId, deviceId);
        List<OrderControl> list = orderControlMapper.selectList(queryWrapper);
        for (OrderControl control : list) {
            //判断是否在执行时间内的控制
            Date now = DateUtils.getNowDate();
            Date startTime = control.getStartTime();
            Date endTime = control.getEndTime();
            if (now.after(startTime) && now.before(endTime) && control.getCount() > 0){
                control.setStatus(1);
            }else {
                control.setStatus(0);
            }
        }
        return list;
    }
    /**
     * 判定是否有操作权限
     * @param deviceId
     * @param modelId
     * @return
     */
    @Override
    public AjaxResult judgeThingsModel(Long deviceId, Long modelId) {
        Long userId = SecurityUtils.getUserId();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        Long deptUserId = loginUser.getDeptUserId();
        //这里排除管理员和终端用户和演示账号
        if (!userId.equals(deptUserId) && !Objects.isNull(loginUser.getDeptId()) && !"fastbee".equals(loginUser.getUsername())) {
            LambdaQueryWrapper<OrderControl> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(OrderControl::getUserId, userId);
            queryWrapper.eq(OrderControl::getDeviceId, deviceId);
            List<OrderControl> list = orderControlMapper.selectList(queryWrapper);
            for (OrderControl control : list) {
                //判断是否在执行时间内的控制
                Date now = DateUtils.getNowDate();
                Date startTime = control.getStartTime();
                Date endTime = control.getEndTime();
                if (now.after(startTime) && now.before(endTime) && control.getCount() > 0) {
                    String selectOrder = control.getSelectOrder();
                    String[] ids = selectOrder.split(",");
                    if (Arrays.asList(ids).contains(modelId + "")) {
                        return AjaxResult.success();
                    }
                }
            }
            return AjaxResult.error(MessageUtils.message("device.order.control.no.permission"));
        }
        return AjaxResult.success();
    }
}
