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

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
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.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.mapper.CameraMapper;
import com.huimv.video.video.service.ICameraAreaService;
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 CameraFunctionServiceImpl extends ServiceImpl<CameraFunctionMapper, CameraFunction> implements ICameraFuntcionService {

    @Autowired
    private CameraMapper cameraMapper;

    @Autowired
    private CameraFunctionAreaMapper cameraFunctionAreaMapper;

    @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<CameraFunction> list = page(new Page<>(current, size), new QueryWrapper<CameraFunction>().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) {
            CameraFunction byId = this.getById(id);


            List<CameraFunctionArea> cameraFunctionAreas = cameraFunctionAreaMapper.selectList(new QueryWrapper<CameraFunctionArea>().like("function_ids", id));
            for (CameraFunctionArea cameraFunctionArea : cameraFunctionAreas) {

                String functionIds = cameraFunctionArea.getFunctionIds();
                functionIds = functionIds.replaceAll(id+",","");
                functionIds = functionIds.replaceAll("1"+id,"");

                cameraFunctionArea.setFunctionIds(functionIds);
                cameraFunctionAreaMapper.updateById(cameraFunctionArea);
            }


            Integer areaId = cameraMapper.selectCount(new QueryWrapper<Camera>().eq("function_id", id));
            if (areaId > 0) {
                return new Result(10001, "删除失败," + byId.getFunctionName() + "仍然存在摄像头", false);
            }
        }
        this.removeByIds(ids);
        return new Result(10000, "删除成功", true);
    }

    public static void main(String[] args) {
        String str  ="1,2,3";

            System.out.println(str);
    }

    @Override
    public Result lsitAndCamera(Integer farmId) {
        List endList = new ArrayList();
        List<CameraFunctionArea> cameraFunctionAreas = cameraFunctionAreaMapper.selectList(new QueryWrapper<CameraFunctionArea>().eq("farm_id", farmId));
        for (CameraFunctionArea cameraFunctionArea : cameraFunctionAreas) {
            String functionIds = cameraFunctionArea.getFunctionIds();
            Map functionAreaMap = new HashMap();
            functionAreaMap.put("cameraName", cameraFunctionArea.getFunctionName());
            functionAreaMap.put("id", cameraFunctionArea.getId());
            functionAreaMap.put("functionList", null);
            if (StringUtils.isNotBlank(functionIds)){
                List<CameraFunction> cameraFunctions = this.list(new QueryWrapper<CameraFunction>().in("id", functionIds.split(",")));

                List functionList = new ArrayList();
                for (CameraFunction cameraFunction : cameraFunctions) {
                     Map functionMap = new HashMap<>();
                    functionMap.put("cameraName", cameraFunction.getFunctionName());
                    functionMap.put("id", cameraFunction.getId());
                    functionMap.put("cameraList", cameraMapper.selectList(new QueryWrapper<Camera>().eq("function_id", cameraFunction.getId())));
                    functionList.add(functionMap);

                }
                functionAreaMap.put("functionList", functionList);

            }



            endList.add(functionAreaMap);
        }


        return new Result(ResultCode.SUCCESS, endList);
    }

    @Override
    @Transactional
    public Result addCamera(Map<String, String> params) {
        String functionId = params.get("functionId");
        String carmeraId = params.get("carmeraId");
        String[] split = carmeraId.split(",");
        Camera camera1 = new Camera();
        camera1.setFunctionId(0);
        cameraMapper.update(camera1,new UpdateWrapper<Camera>().eq("function_id",functionId));
        System.out.println(functionId);
        for (String s : split) {
            System.out.println(s);
            Camera camera = new Camera();
            camera.setId(Integer.parseInt(s));
            camera.setFunctionId(Integer.parseInt(functionId));
            cameraMapper.updateById(camera);
        }

        return new Result(10000, "修改成功", true);
    }

    @Override
    public Result deleteCamera(Map<String, String> params) {
        String carmeraId = params.get("carmeraId");
        String[] split = carmeraId.split(",");
        for (String s : split) {
            Camera camera = new Camera();
            camera.setFunctionId(0);
            cameraMapper.updateById(camera);
        }
        return null;
    }

    @Override
    public Result listCamera(Map<String, String> params) {
        String functionId = params.get("functionId");
        List<Camera> function_id = cameraMapper.selectList(new QueryWrapper<Camera>().eq("function_id", functionId));
        List list = new ArrayList();
        for (Camera camera : function_id) {
            list.add(camera.getId());
        }
        return new Result(ResultCode.SUCCESS, list);
    }


}