package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.poi.ExcelUtil;
import cn.cecep.talroad.common.core.web.controller.BasesController;
import cn.cecep.talroad.common.core.web.domain.AjaxResults;
import cn.cecep.talroad.common.log.annotation.Log;
import cn.cecep.talroad.common.log.enums.BusinessType;
import cn.cecep.talroad.common.security.annotation.RequiresPermissions;
import cn.cecep.talroad.common.security.utils.SecurityUtils;
import cn.cecep.talroad.domain.AmVideoDevFi;
import cn.cecep.talroad.domain.MAmVideoInfoOperationLog;
import cn.cecep.talroad.service.IAmVideoDevFiService;
import cn.cecep.talroad.vo.AmVideoDevFiVo;
import cn.cecep.talroad.vo.export.AmVideoDevFiExport;
import cn.cecep.talroad.vo.query.AmVideoDevFiQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.security.InvalidParameterException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;


/**
 * * 开发公司：中节能天融科技有限公司
 * 视频监控Controller 控制层
 *
 * @author szhpt
 * @date 2023-01-10*/
@Api(tags = "监测监控--视频监控")
@RestController
@RequestMapping("/amVideoDevFi")
public class AmVideoDevFiController extends BasesController
{
    @Autowired
    private IAmVideoDevFiService amVideoDevFiService;

    /**
     * 查询视频监控列表
     */
    @ApiOperation(value = "查询视频监控")
    @GetMapping("/list")
    public AjaxResults<IPage<AmVideoDevFi>> list(AmVideoDevFiQuery amVideoDevFi)
    {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)){
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            amVideoDevFi.setFactoryId(factoryId);
        }
        Page<AmVideoDevFi> page = new Page<>(amVideoDevFi.getPageNum(), amVideoDevFi.getPageSize());
        IPage<AmVideoDevFi> list = amVideoDevFiService.page(page, AmVideoDevFiQuery.createLambdaQueryWrapper(amVideoDevFi));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "获取视频监控分组数据")
    @GetMapping("/group/list")
    public AjaxResults<List<AmVideoDevFiVo.DevTypeGroup>> groupList(AmVideoDevFiQuery amVideoDevFi)
    {
        List<AmVideoDevFiVo.DevTypeGroup> list = amVideoDevFiService.getGroupList(amVideoDevFi);
        return AjaxResults.success(list);
    }
    @ApiOperation(value = "获取秸秆视频监控数据")
    @GetMapping("/group/straw")
    public AjaxResults<List<AmVideoDevFi>> groupStraw(@RequestParam(value = "devName",required = false) String devName)
    {
        List<AmVideoDevFi> list = amVideoDevFiService.list(new QueryWrapper<AmVideoDevFi>().eq("dev_type","秸秆").isNull("factory_id").like(StringUtils.isNotEmpty(devName),"dev_name",devName).orderByAsc("dev_id"));
        return AjaxResults.success(list);
    }


    /**
     * 导出视频监控列表
     */
    @ApiOperation(value = "导出视频监控")
    @Log(title = "视频监控", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AmVideoDevFi amVideoDevFi)
    {
        List<AmVideoDevFi> list = amVideoDevFiService.selectAmVideoDevFiList(amVideoDevFi);
        ExcelUtil<AmVideoDevFi> util = new ExcelUtil<AmVideoDevFi>(AmVideoDevFi.class);
        util.exportExcel(response, list, "视频监控数据");
    }
    @ApiOperation(value = "获取视频监控搜索框内容--企业端")
    @GetMapping("/listFactoryVideo")
    public AjaxResults<List<AmVideoDevFiExport>> listFactoryVideo(AmVideoDevFiQuery amVideoDevFi)
    {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)){
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            amVideoDevFi.setFactoryId(factoryId);
            amVideoDevFi.setIsStatus("1");
        }
        List<AmVideoDevFiExport> list = amVideoDevFiService.selectAmVideoDevFiSelectList(AmVideoDevFiQuery.createLambdaQueryWrapperFactory(amVideoDevFi));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "获取视频监控搜索框内容-排污许可设施--企业端")
        @GetMapping("/listFactoryXkVideo")
    public AjaxResults<List<AmVideoDevFiExport>> listFactoryXkVideo(AmVideoDevFiQuery amVideoDevFi)
    {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)){
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            amVideoDevFi.setFactoryId(factoryId);
            amVideoDevFi.setIndustry("XK");
            amVideoDevFi.setIsStatus("1");
        }
        List<AmVideoDevFiExport> list = amVideoDevFiService.selectAmVideoDevFiSelectXkList(AmVideoDevFiQuery.createLambdaQueryWrapperXK(amVideoDevFi));
        return AjaxResults.success(list);
    }

    @ApiOperation(value = "查询视频监控-企业端")
    @GetMapping("/listFactory")
    public AjaxResults<IPage<AmVideoDevFiExport>> listFactory(AmVideoDevFiQuery amVideoDevFi)
    {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)){
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            amVideoDevFi.setFactoryId(factoryId);
            amVideoDevFi.setIsStatus("1");
        }
        Page<AmVideoDevFi> page = new Page<>(amVideoDevFi.getPageNum(), amVideoDevFi.getPageSize());
        IPage<AmVideoDevFiExport> list = amVideoDevFiService.selectDeviceNameById(page, AmVideoDevFiQuery.createLambdaQueryWrapperFactory(amVideoDevFi));
        return AjaxResults.success(list);
    }
    @ApiOperation(value = "导出视频监控-企业端")
    @Log(title = "导出视频监控-企业端", businessType = BusinessType.EXPORT)
    @PostMapping("/exportFactory")
    public void exportFactory(HttpServletResponse response, AmVideoDevFiQuery amVideoDevFi)
    {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)){
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            amVideoDevFi.setFactoryId(factoryId);
            amVideoDevFi.setIsStatus("1");
        }
        List<AmVideoDevFiExport> list = amVideoDevFiService.selectAmVideoDevFiSelectList(AmVideoDevFiQuery.createLambdaQueryWrapperFactory(amVideoDevFi));
        ExcelUtil<AmVideoDevFiExport> util = new ExcelUtil<AmVideoDevFiExport>(AmVideoDevFiExport.class);
        util.exportExcel(response, list, "视频监控数据");
    }

    @ApiOperation(value = "修改视频监控-企业端")
    @Log(title = "视频监控", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editFactory")
    public AjaxResults<Boolean> editFactory(@RequestBody List<AmVideoDevFiExport> amVideoDevFiVo) {
        if(amVideoDevFiVo.size()==0){
            throw new InvalidParameterException("没有修改的内容");
        }
       List<AmVideoDevFi> saveList = new ArrayList<AmVideoDevFi>();
       List<AmVideoDevFiExport> saveExportList = new ArrayList<AmVideoDevFiExport>();
       for(AmVideoDevFiExport avdi:amVideoDevFiVo){
           AmVideoDevFi amVideoDevFi = amVideoDevFiService.getById(avdi.getId());
           AmVideoDevFiExport amVideoDevFiExport = amVideoDevFiService.getByExport(avdi.getId());
           if(ObjectUtil.isEmpty(amVideoDevFi)){
               throw new CommonException(StrUtil.format("根据id{}查询不到数据", avdi.getId()));
           }
           amVideoDevFi.setDeviceId(avdi.getDeviceId());
           saveList.add(amVideoDevFi);
           saveExportList.add(amVideoDevFiExport);
       }
        amVideoDevFiService.saveLog(amVideoDevFiVo,saveExportList);
        //赋值
        return toAjax(amVideoDevFiService.saveOrUpdateBatch(saveList));
    }

    @ApiOperation(value = "操作日志列表-视频设备绑定排污可设备信息")
    @PostMapping("/getMAmVideoInfoOperationLogList")
    public AjaxResults<IPage<MAmVideoInfoOperationLog>> getMAmVideoInfoOperationLogList(AmVideoDevFiQuery amVideoDevFiVo) {
        try {
            IPage<MAmVideoInfoOperationLog> list = amVideoDevFiService.getMAmVideoInfoOperationLogList(amVideoDevFiVo);
            return AjaxResults.success(list);
        } catch (Exception e) {
            return AjaxResults.error("操作日志列表获取失败！");
        }
    }

    /**
     * 获取视频监控详细信息
     */
    @ApiOperation(value = "获取视频监控详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResults<AmVideoDevFiVo> getInfo(@PathVariable("id") Long id) {
        return AjaxResults.success(amVideoDevFiService.getInfo(id));
    }
    /**
     * 新增视频监控
     */
    @ApiOperation(value = "新增视频监控")
    @Log(title = "视频监控", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody AmVideoDevFi amVideoDevFi) {
        return toAjax(amVideoDevFiService.save(amVideoDevFi));
    }
    /**
     * 修改视频监控
     */
    @ApiOperation(value = "修改视频监控")
    @Log(title = "视频监控", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody AmVideoDevFiVo amVideoDevFiVo) {
        if(ObjectUtil.isEmpty(amVideoDevFiVo)){
            throw new InvalidParameterException("参数为空");
        }
        if(ObjectUtil.isEmpty(amVideoDevFiVo.getId())){
            throw new InvalidParameterException("参数id为空");
        }
        AmVideoDevFi amVideoDevFi = amVideoDevFiService.getById(amVideoDevFiVo.getId());
        if(ObjectUtil.isEmpty(amVideoDevFi)){
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", amVideoDevFiVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(amVideoDevFiVo,amVideoDevFi);
        return toAjax(amVideoDevFiService.saveOrUpdate(amVideoDevFi));
    }

    /**
     * 删除视频监控
     */
    @ApiOperation(value = "删除视频监控")
    @RequiresPermissions("talroad:fi:remove")
    @Log(title = "视频监控", businessType = BusinessType.DELETE)
    @PostMapping("/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
        return toAjax(amVideoDevFiService.removeByIds(Arrays.asList(ids)));
    }

}
