package com.huimv.video.video.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huimv.common.utils.Result;
import com.huimv.common.utils.ResultCode;
import com.huimv.video.video.entity.Camera;
import com.huimv.video.video.entity.CameraArea;
import com.huimv.video.video.entity.CameraFunction;
import com.huimv.video.video.entity.CameraFunctionArea;
import com.huimv.video.video.mapper.CameraFunctionAreaMapper;
import com.huimv.video.video.mapper.CameraFunctionMapper;
import com.huimv.video.video.service.ICameraFuntcionAreaService;
import com.huimv.video.video.service.ICameraFuntcionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

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

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author astupidcoder
 * @since 2021-09-13
 */
@Service
public class CameraFunctionAreaServiceImpl extends ServiceImpl<CameraFunctionAreaMapper, CameraFunctionArea> implements ICameraFuntcionAreaService {

    @Autowired
    private CameraFunctionAreaMapper cameraFunctionAreaMapper;

    @Autowired
    private CameraFunctionMapper cameraFunctionMapper;

    @Override
    public Result cameraList(Map<String, Object> params) {
        Integer current = (Integer) params.get("current");
        Integer size = (Integer) params.get("size");
        String functionaName = (String) params.get("functionaName");
        Integer farmId = (Integer) params.get("farmId");

        IPage<CameraFunctionArea> list = page(new Page<>(current, size), new QueryWrapper<CameraFunctionArea>().like(StringUtils.isNotBlank(functionaName), "area_name", functionaName).eq("farm_id", farmId));
        return new Result(ResultCode.SUCCESS, list);
    }

    @Override
    @Transactional
    public Result removeIds(List<Integer> ids) {
        for (Integer id : ids) {
            CameraFunctionArea byId = this.getById(id);
            if (StringUtils.isNotBlank(byId.getFunctionIds()) ) {
                return new Result(10001, "删除失败," + byId.getFunctionName() + "仍然存在区域", false);
            }
        }
        this.removeByIds(ids);
        return new Result(10000, "删除成功", true);
    }

    @Override
    public Result addFunction(Map<String, String> params) {
        String functionAreaId = params.get("functionAreaId");

        String functionIds = params.get("functionIds");

        CameraFunctionArea cameraFunctionArea = new CameraFunctionArea();
        cameraFunctionArea.setId(Integer.parseInt(functionAreaId));
        cameraFunctionArea.setFunctionIds(functionIds);
        this.updateById(cameraFunctionArea);
        return new Result(10000, "添加成功", true);
    }
}