package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.exception.CommonException;
import cn.cecep.talroad.common.core.utils.DateUtils;
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.utils.SecurityUtils;
import cn.cecep.talroad.domain.BMainMonitorPointOperationLog;
import cn.cecep.talroad.domain.MAmVideoCoordinateOperationLog;
import cn.cecep.talroad.domain.MAmVideoDevFi;
import cn.cecep.talroad.enums.OperationTypeEnum;
import cn.cecep.talroad.service.IMAmVideoCoordinateOperationLogService;
import cn.cecep.talroad.service.IMAmVideoDevFiService;
import cn.cecep.talroad.vo.BasDeviceExcelVo;
import cn.cecep.talroad.vo.MAmVideoCoordinateOperationLogQuery;
import cn.cecep.talroad.vo.MAmVideoDevFiExcelVo;
import cn.cecep.talroad.vo.MAmVideoDevFiVo;
import cn.cecep.talroad.vo.query.MAmVideoDevFiQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.CollectionUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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.*;
import java.util.stream.IntStream;


/**
 * * 开发公司：中节能天融科技有限公司
 * 基础信息 与 产治排跟踪--视频设施清单Controller 控制层
 *
 * @author szhpt
 * @date 2023-02-10
 */
@Api(tags = "基础信息 与 产治排跟踪--视频设施清单")
@RestController
@RequestMapping("/mamVideoDevFi")
public class MAmVideoDevFiController extends BasesController {
    @Autowired
    private IMAmVideoDevFiService mAmVideoDevFiService;
    @Autowired
    private IMAmVideoCoordinateOperationLogService imAmVideoCoordinateOperationLogService;

    /**
     * 查询基础信息 与 产治排跟踪--视频设施清单列表
     */
    @ApiOperation(value = "查询基础信息 与 产治排跟踪--视频设施清单")
    @GetMapping("/list")
    public AjaxResults<IPage<MAmVideoDevFi>> list(MAmVideoDevFiQuery mAmVideoDevFi) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            mAmVideoDevFi.setFactoryId(factoryId);
            //mAmVideoDevFi.setStatus("1");
        }
        Page<MAmVideoDevFi> page = new Page<>(mAmVideoDevFi.getPageNum(), mAmVideoDevFi.getPageSize());
        IPage<MAmVideoDevFi> list = mAmVideoDevFiService.page(page, MAmVideoDevFiQuery.createLambdaQueryWrapper(mAmVideoDevFi));
        return AjaxResults.success(list);
    }

    /**
     * 查询基础信息 与 产治排跟踪--视频设施清单列表
     */
    @ApiOperation(value = "视频设施下拉框")
    @GetMapping("/mAmVideoDevFiSelect")
    public AjaxResults<List<MAmVideoDevFi>> mAmVideoDevFiSelect(MAmVideoDevFiQuery mAmVideoDevFi) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            mAmVideoDevFi.setFactoryId(factoryId);
            mAmVideoDevFi.setStatus("1");
        }
        List<MAmVideoDevFi> list = mAmVideoDevFiService.list(MAmVideoDevFiQuery.createLambdaQueryWrapper(mAmVideoDevFi));
        return AjaxResults.success(list);
    }


    /**
     * 导出基础信息 与 产治排跟踪--视频设施清单列表
     */
    @ApiOperation(value = "导出基础信息 与 产治排跟踪--视频设施清单")
    @Log(title = "基础信息 与 产治排跟踪--视频设施清单", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody MAmVideoDevFi mAmVideoDevFi) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            mAmVideoDevFi.setFactoryId(factoryId);
            mAmVideoDevFi.setStatus("1");
        }
        List<MAmVideoDevFi> list = mAmVideoDevFiService.selectMAmVideoDevFiList(mAmVideoDevFi);

        List<MAmVideoDevFiExcelVo> mAmVideoDevFiExcelVoArrayList = new ArrayList<>();
        PrimitiveIterator.OfInt iterator = IntStream.iterate(1, k -> k + 1).iterator();
        list.forEach(e -> {
            MAmVideoDevFiExcelVo mAmVideoDevFiExcelVo = new MAmVideoDevFiExcelVo();
            BeanUtil.copyProperties(e, mAmVideoDevFiExcelVo);
            mAmVideoDevFiExcelVo.setSerialnumber(iterator.next());
            mAmVideoDevFiExcelVoArrayList.add(mAmVideoDevFiExcelVo);
        });
        ExcelUtil<MAmVideoDevFiExcelVo> util = new ExcelUtil<MAmVideoDevFiExcelVo>(MAmVideoDevFiExcelVo.class);
        util.exportExcel(response, mAmVideoDevFiExcelVoArrayList, "视频坐标");
    }

    /**
     * 获取基础信息 与 产治排跟踪--视频设施清单详细信息
     */
    @ApiOperation(value = "获取基础信息 与 产治排跟踪--视频设施清单详细信息")
    @GetMapping(value = "/{id}")
    public AjaxResults<MAmVideoDevFi> getInfo(@PathVariable("id") Integer id) {
        return AjaxResults.success(mAmVideoDevFiService.getById(id));
    }

    /**
     * 新增基础信息 与 产治排跟踪--视频设施清单
     */
    @ApiOperation(value = "新增基础信息 与 产治排跟踪--视频设施清单")
    @Log(title = "基础信息 与 产治排跟踪--视频设施清单", businessType = BusinessType.INSERT)
    @PostMapping(value = "/add")
    public AjaxResults<Boolean> add(@RequestBody MAmVideoDevFi mAmVideoDevFi) {
        return toAjax(mAmVideoDevFiService.save(mAmVideoDevFi));
    }

    /**
     * 修改基础信息 与 产治排跟踪--视频设施清单
     */
    @ApiOperation(value = "批量修改基础信息 与 产治排跟踪--视频设施清单")
    @Log(title = "基础信息 与 产治排跟踪--视频设施清单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/editBatch")
    public AjaxResults<Boolean> editBatch(@RequestBody MAmVideoDevFiVo[] mAmVideoDevFiVoArr) {
        if (mAmVideoDevFiVoArr ==null || mAmVideoDevFiVoArr.length < 1) {
            throw new InvalidParameterException("参数为空");
        }

        mAmVideoDevFiService.editBatch(mAmVideoDevFiVoArr);
        return toAjax(true);
    }

    /**
     * 修改基础信息 与 产治排跟踪--视频设施清单
     */
    @ApiOperation(value = "修改单个基础信息 与 产治排跟踪--视频设施清单")
    @Log(title = "基础信息 与 产治排跟踪--视频设施清单", businessType = BusinessType.UPDATE)
    @PostMapping(value = "/edit")
    public AjaxResults<Boolean> edit(@RequestBody MAmVideoDevFiVo mAmVideoDevFiVo) {
        if (ObjectUtil.isEmpty(mAmVideoDevFiVo)) {
            throw new InvalidParameterException("参数为空");
        }
        if (ObjectUtil.isEmpty(mAmVideoDevFiVo.getId())) {
            throw new InvalidParameterException("参数id为空");
        }
        MAmVideoDevFi mAmVideoDevFi = mAmVideoDevFiService.getById(mAmVideoDevFiVo.getId());
        if (ObjectUtil.isEmpty(mAmVideoDevFi)) {
            throw new CommonException(StrUtil.format("根据id{}查询不到数据", mAmVideoDevFiVo.getId()));
        }
        //赋值
        BeanUtil.copyProperties(mAmVideoDevFiVo, mAmVideoDevFi);
        return toAjax(mAmVideoDevFiService.saveOrUpdate(mAmVideoDevFi));
    }

    /**
     *
     * 删除基础信息 与 产治排跟踪--视频设施清单
     */
    @ApiOperation(value = "删除基础信息 与 产治排跟踪--视频设施清单")
//    @RequiresPermissions("talroad:mamVideoDevFi:remove")
    @Log(title = "基础信息 与 产治排跟踪--视频设施清单", businessType = BusinessType.DELETE)
    @PostMapping("/remove/{ids}")
    public AjaxResults<Boolean> remove(@PathVariable Long[] ids) {
        return toAjax(mAmVideoDevFiService.removeByIds(Arrays.asList(ids)));
    }

    /**
     * 操作日志
     */
    @ApiOperation(value = "操作日志")
    @GetMapping("/editLogList")
    public AjaxResults<IPage<MAmVideoCoordinateOperationLog>> editLogList(MAmVideoCoordinateOperationLogQuery mAmVideoCoordinateOperationLogQuery) {
        Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
        if(ObjectUtil.equal(roleId,10L)) {
            String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
            mAmVideoCoordinateOperationLogQuery.setFactoryId(factoryId);
        }
        Page<MAmVideoCoordinateOperationLog> page = new Page<>(mAmVideoCoordinateOperationLogQuery.getPageNum(), mAmVideoCoordinateOperationLogQuery.getPageSize());
        IPage<MAmVideoCoordinateOperationLog> list = imAmVideoCoordinateOperationLogService.page(page, mAmVideoCoordinateOperationLogQuery.createLambdaQueryWrapper(mAmVideoCoordinateOperationLogQuery));
        return AjaxResults.success(list);
    }
}
