package com.huimv.env.admin.controller;


import cn.hutool.core.util.ObjectUtil;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.env.admin.common.utils.Result;
import com.huimv.env.admin.common.utils.ResultCode;
import com.huimv.env.admin.common.utils.ResultUtil;
import com.huimv.env.admin.entity.BasePigpen;
import com.huimv.env.admin.entity.TerminalPort;
import com.huimv.env.admin.entity.dto.BasePigpenDto;
import com.huimv.env.admin.mapper.BasePigpenMapper;
import com.huimv.env.admin.mapper.TerminalMapper;
import com.huimv.env.admin.mapper.TerminalPortMapper;
import com.huimv.env.admin.service.IBasePigpenService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author author
 * @since 2024-05-06
 */
@RestController
@RequestMapping("/basePigpen")
@CrossOrigin
public class BasePigpenController {
    @Autowired
    private IBasePigpenService basePigpenService;

    @Autowired
    private BasePigpenMapper basePigpenMapper;
    @Autowired
    private TerminalPortMapper portMapper;

    @PostMapping( "/addPigpen")
    public Result addPigpen(@RequestBody BasePigpenDto basePigpenDto) {
        return basePigpenService.addPigpen(basePigpenDto);
    }

    @PostMapping( "/addUnit")
    public Result addUnit(@RequestBody Map<String,String> map) {
        String farmId = map.get("farmId");
        String parentId = map.get("parentId");
        int buildNum = Integer.valueOf(map.get("buildNum"));
        String stageCode = map.get("stageCode");
        BasePigpen parent = basePigpenService.getById(parentId);
        String parentIds = parent.getOther2() + "," + parentId;
        int maxBuildNum = basePigpenMapper.getMaxBuildNum(parentId);
        List<BasePigpen> basePigpenList = new ArrayList<>();
        for (int i = 1; i <= buildNum; i++) {
            int num = maxBuildNum+i;
            BasePigpen basePigpen = new BasePigpen();
            basePigpen.setBuildName(parent.getBuildName()+num+"单元");
            basePigpen.setParentId(Integer.valueOf(parentId));
            basePigpen.setFType(2);
//            basePigpen.setStageCode(Integer.valueOf(stageCode));
            basePigpen.setFarmId(Integer.valueOf(farmId));
            basePigpen.setOther4(num);
            basePigpen.setOther2(parentIds);
            basePigpenList.add(basePigpen);
        }

        if ( basePigpenService.saveBatch(basePigpenList)){
            return ResultUtil.addResult(1);
        }
        return ResultUtil.addResult(0);
    }

    @PostMapping( "/addDoor")
    public Result addDoor(@RequestBody Map<String,String> map) {
        String farmId = map.get("farmId");
        String parentId = map.get("parentId");
        String buildName = map.get("buildName");
        BasePigpen parent = basePigpenService.getById(parentId);
        String parentIds = parent.getOther2() + "," + parentId;

        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("farm_id", farmId).eq("other2", parentIds).eq("build_name", buildName);
        if (ObjectUtil.isNotEmpty(basePigpenService.getOne(queryWrapper))) {
            return ResultUtil.addResult(0);
        }
        BasePigpen basePigpen = new BasePigpen();
        basePigpen.setBuildName(parent.getBuildName()+buildName);
        basePigpen.setParentId(Integer.valueOf(parentId));
        basePigpen.setFType(4);
        basePigpen.setFarmId(Integer.valueOf(farmId));
        basePigpen.setOther2(parentIds);
        basePigpenService.save(basePigpen);
        return ResultUtil.addResult(1);
    }

    @PostMapping( "/deleteDoor")
    public Result deleteDoor(@RequestBody Map<String,String> map) {
        String id = map.get("id");
        Integer count = portMapper.selectCount(new QueryWrapper<TerminalPort>().eq("location_id", id));
        if (count > 0) {
            return ResultUtil.deleteResult(0);
        }
        basePigpenService.removeById(id);
        return ResultUtil.deleteResult(1);
    }

    @PostMapping( "/updateDoor")
    public Result updateDoor(@RequestBody Map<String,String> map) {
        String id = map.get("id");
        String buildName = map.get("buildName");
        BasePigpen basePigpen = basePigpenService.getById(id);
        int count = basePigpenService.count(new QueryWrapper<BasePigpen>().ne("id", id)
                .eq("f_type", 4).eq("farm_id", basePigpen.getFarmId())
                .eq("other2", basePigpen.getOther2()).eq("buildName", buildName));
        if (count > 0) {
            return ResultUtil.updateResult(0);
        }
        basePigpen.setBuildName(buildName);
        basePigpenService.updateById(basePigpen);
        return ResultUtil.updateResult(1);
    }


    @PostMapping( "/updatePigpen")
    public Result updatePigpen(@RequestBody BasePigpen basePigpen) {
        return  basePigpenService.updatePigpen(basePigpen);
    }

    @PostMapping( "/deviceCount")
    public Result deviceCount(@RequestBody Map<String,Integer> map) {
        return  basePigpenService.deviceCount(map);
    }

    @PostMapping( "/updateUnit")
    public Result updateUnit(@RequestBody BasePigpen basePigpen) {
        Integer  num = basePigpenService.count(new QueryWrapper<BasePigpen>().eq("build_name", basePigpen.getBuildName())
                .eq("farm_id",basePigpen.getFarmId()).ne("id", basePigpen.getId()));
        if (num  > 0){
            return new Result(10001, "栋舍名称已存在！", false);
        }
        if ( basePigpenService.updateById(basePigpen)){
            return ResultUtil.addResult(1);
        }
        return ResultUtil.addResult(0);
    }

    @PostMapping( "/deletePigpen")
    public Result deletePigpen(@RequestBody Map<String,Integer> map) {
        return  basePigpenService.deletePigpen(map);
    }

    @PostMapping( "/listPigpen")
    public Result listPigpen(@RequestBody Map<String,Integer> map) {
        return  basePigpenService.listPigpen(map);
    }
    @PostMapping( "/getGateway")
    public Result getGateway(@RequestBody Map<String,Integer> map) {
        return  basePigpenService.getGateway(map);
    }
    @RequestMapping(value = "/list",method = RequestMethod.POST)
    public Result list(@RequestBody Map<String, Object> paramsMap) {
        System.out.println("paramsMap>>"+paramsMap.toString());
        String type = paramsMap.get("type").toString();

        String farmCode = "";
        if (paramsMap.get("farmId") != null) {
            farmCode = paramsMap.get("farmId")+"";
        }
        String stageCode = "";
        if (paramsMap.get("stageCode") != null) {
            stageCode = paramsMap.get("stageCode")+"";
        }
        String buildName = "";
        if (paramsMap.get("buildName") != null) {
            buildName = paramsMap.get("buildName")+"";
        }
        return basePigpenService.list(farmCode,buildName,stageCode,type);
    }

    //根据栋id拿到所有的层
    @PostMapping( "/listCeng")
    public Result listCeng(@RequestBody Map<String,Integer> map) {
        Integer id = map.get("id");
        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("other2", id).eq("f_type", 3);
        return new Result(ResultCode.SUCCESS, basePigpenMapper.selectList(queryWrapper));
    }

    //根据层id拿到所有的户
    @PostMapping( "/listDoor")
    public Result listDoor(@RequestBody Map<String,Integer> map) {
        Integer id = map.get("id");
        QueryWrapper<BasePigpen> queryWrapper = new QueryWrapper<>();
        queryWrapper.like("other2", id).eq("f_type", 4);
        return new Result(ResultCode.SUCCESS, basePigpenMapper.selectList(queryWrapper));
    }
}
