package com.huimv.beeboxs.dahuaVideo.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.huimv.beeboxs.common.utils.Result;
import com.huimv.beeboxs.common.utils.ResultCode;
import com.huimv.beeboxs.entity.FarmVideoAccount;
import com.huimv.beeboxs.entity.HiveBaseStation;
import com.huimv.beeboxs.mapper.FarmVideoAccountMapper;
import com.huimv.beeboxs.mapper.HiveBaseStationMapper;
import com.huimv.beeboxs.service.FarmVideoAccountService;
import com.huimv.beeboxs.service.IBaseHiveFarmService;
import com.huimv.beeboxs.service.IHiveBaseStationService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * TODO
 *添加大华的账号到牧场信息上
 * @author CodeYang.
 * @date 2022/4/24 16:16
 */


@CrossOrigin
@RestController
@RequestMapping(value = "/dahua")
public class SetDahuaAccountToFarmInfoController {


    @Autowired
    private FarmVideoAccountMapper farmVideoAccountMapper;

    @Autowired
    private IBaseHiveFarmService  iBaseHiveFarmService;

 

    @Autowired
    private IHiveBaseStationService baseStationService;



    @Autowired
    private HiveBaseStationMapper hiveBaseStationMapper;


    @Autowired
    private FarmVideoAccountService   farmVideoAccountService;

    /**
     * 给牧场绑定大华账户
     * @param map
     * @return
     */
    @PostMapping("/showDahuaaccount")
    public Result changeDirection(@RequestBody Map map) {

        QueryWrapper<FarmVideoAccount> FarmVideoAccountQueryWrapper = new QueryWrapper<>();
        FarmVideoAccountQueryWrapper.eq("farm_id", map.get("farmId"));
        FarmVideoAccount farmVideoAccounts = farmVideoAccountMapper.selectOne(FarmVideoAccountQueryWrapper);

        return new Result(ResultCode.SUCCESS, farmVideoAccounts);

    }


        /**
         * 给牧场绑定大华账户  账号唯一
         * @param map
         * @return
         */
        @PostMapping("/addDahuaAccount")
        public Result addDahuaaccount(@RequestBody Map map){

            QueryWrapper<FarmVideoAccount> FarmVideoAccountQueryWrapper = new QueryWrapper<>();
            FarmVideoAccountQueryWrapper.eq("farm_id",map.get("farmId"));
            FarmVideoAccount farmVideoAccounts = farmVideoAccountMapper.selectOne(FarmVideoAccountQueryWrapper);
            if(farmVideoAccounts!=null){
                return new Result(ResultCode.FAIL,"一个牧场只能绑定一个账号, 请勿重复添加");
            }
            FarmVideoAccount farmVideoAccount = new FarmVideoAccount();
            farmVideoAccount.setFarmId((Integer)map.get("farmId"));
            farmVideoAccount.setDahuaAppid((String) map.get("DahuaAppid"));
            farmVideoAccount.setDahuaSecret((String) map.get("DahuaSecret"));
            farmVideoAccount.setFarmName( iBaseHiveFarmService.getById((Integer)map.get("farmId")).getFarmName());
            farmVideoAccount.setRemark((String) map.get("Remark"));
            farmVideoAccountService.save(farmVideoAccount);
            return new Result(ResultCode.SUCCESS  );

}

    /**
     * 给牧场绑定大华账户
     * @param
     * @return
     */
    @PostMapping("/updateDahuaAccount")
    public Result updateDahuaaccount(@RequestBody Map map){
        FarmVideoAccount farmVideoAccount = farmVideoAccountService.getOne(new QueryWrapper<FarmVideoAccount>().eq("farm_id", map.get("farmId")));
        System.out.println(farmVideoAccount);
        //只能更改大华的信息  牧场信息不能变
        farmVideoAccount.setDahuaAppid((String) map.get("DahuaAppid"));
        farmVideoAccount.setDahuaSecret((String) map.get("DahuaSecret"));
        farmVideoAccount.setFarmName( iBaseHiveFarmService.getById((Integer)map.get("farmId")).getFarmName());
        farmVideoAccount.setRemark((String) map.get("Remark"));
        farmVideoAccountService.updateById(farmVideoAccount);
        return new Result(ResultCode.SUCCESS );
    }
     //绑定摄像头到基站


    @PostMapping("/updateCameraToDevice")
    public Result updateVideo(@RequestBody Map map){

        HiveBaseStation hex = baseStationService.getOne(new QueryWrapper<HiveBaseStation>().eq("id", (String)map.get("Id")));
        hex.setDeviceId((String) map.get("deviceId"));
        hex.setChannelId((String) map.get("channelId"));
        hex.setChannelName((String) map.get("channelName"));
        baseStationService.updateById(hex);
        return new Result(10000,"操作成功",true);
    }

}
