package cn.cecep.talroad.controller;

import cn.cecep.talroad.common.core.utils.bean.BeanUtils;
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.PcFactoryAccessControl;
import cn.cecep.talroad.service.IPcFactoryAccessControlService;
import cn.cecep.talroad.vo.PcFactoryAccessControlVo;
import cn.cecep.talroad.vo.query.PcFactoryAccessControlQuery;
import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
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.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;
import java.util.PrimitiveIterator;
import java.util.stream.IntStream;

/**
 * @Description: 企业门禁信息
 * @Author: code-generate
 * @Date:   2023-07-18
 * @Version: V1.0
 */
@Api(tags = "企业门禁信息")
@RestController
@RequestMapping("/pcFactoryAccessControl")
public class PcFactoryAccessControlController  extends BasesController{
	@Autowired
	private IPcFactoryAccessControlService pcFactoryAccessControlService;
	
    /**
	 * 查询企业门禁信息列表
	 */
	@ApiOperation(value = "查询企业门禁坐标信息列表")
	@GetMapping("/list")
	public AjaxResults<IPage<PcFactoryAccessControl>> list(PcFactoryAccessControlQuery pcFactoryAccessControl) {
		Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
		if(ObjectUtil.equal(roleId,10L)) {
			String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
			pcFactoryAccessControl.setFactoryId(factoryId);
		}
		Page<PcFactoryAccessControl> page = new Page<>(pcFactoryAccessControl.getPageNum(), pcFactoryAccessControl.getPageSize());
		IPage<PcFactoryAccessControl> list = pcFactoryAccessControlService.page(page, PcFactoryAccessControlQuery.createLambdaQueryWrapper(pcFactoryAccessControl));
		return AjaxResults.success(list);
	}

	/**
	 * 导出企业门禁信息列表
	 */
	@ApiOperation(value = "导出企业门禁坐标信息列表")
	@Log(title = "企业门禁坐标信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, @RequestBody PcFactoryAccessControlQuery pcFactoryAccessControlQuery) {
		Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
		if(ObjectUtil.equal(roleId,10L)) {
			String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
			pcFactoryAccessControlQuery.setFactoryId(factoryId);
		}
		List<PcFactoryAccessControl> list = pcFactoryAccessControlService.list(PcFactoryAccessControlQuery.createLambdaQueryWrapper(pcFactoryAccessControlQuery));

		List<PcFactoryAccessControlVo> pcFactoryAccessControlList = new ArrayList<>();
		PrimitiveIterator.OfInt iterator = IntStream.iterate(1, k -> k + 1).iterator();
		list.forEach(e -> {
			PcFactoryAccessControlVo pcFactoryAccessControlVo = new PcFactoryAccessControlVo();
			BeanUtil.copyProperties(e, pcFactoryAccessControlVo);
			pcFactoryAccessControlVo.setSerialnumber(iterator.next());
			pcFactoryAccessControlList.add(pcFactoryAccessControlVo);
		});
		ExcelUtil<PcFactoryAccessControlVo> util = new ExcelUtil<>(PcFactoryAccessControlVo.class);
		util.exportExcel(response, pcFactoryAccessControlList, "企业门禁信息");
	}


	/**
	 * 查询企业门禁下拉框
	 */
	@ApiOperation(value = "企业门禁下拉框")
	@GetMapping("/factoryAccessControlSelect")
	public AjaxResults<List<PcFactoryAccessControl>> factoryAccessControlSelect(PcFactoryAccessControlQuery pcFactoryAccessControl) {
		Long roleId = SecurityUtils.getLoginUser().getSysUser().getRoles().get(0).getRoleId();
		if(ObjectUtil.equal(roleId,10L)) {
			String factoryId = SecurityUtils.getLoginUser().getSysUser().getFactoryId();
			pcFactoryAccessControl.setFactoryId(factoryId);
		}
		List<PcFactoryAccessControl> list = pcFactoryAccessControlService.list(PcFactoryAccessControlQuery.createLambdaQueryWrapper(pcFactoryAccessControl).groupBy("access_control_name"));
		return AjaxResults.success(list);
	}

	/**
	 * 批量修改企业门禁坐标信息
	 */
	@ApiOperation(value = "批量修改企业门禁坐标信息")
	@Log(title = "企业门禁坐标信息", businessType = BusinessType.UPDATE)
	@PostMapping(value = "/editBatchCoordinate")
	public AjaxResults<Boolean> editBatch(@RequestBody PcFactoryAccessControl[] pcFactoryAccessControlArr) {
		if (pcFactoryAccessControlArr ==null || pcFactoryAccessControlArr.length < 1) {
			throw new InvalidParameterException("参数为空");
		}
		pcFactoryAccessControlService.editBatch(pcFactoryAccessControlArr);
		return toAjax(true);
	}

	/**
	 * 获取企业门禁信息详细信息
	 */
	@ApiOperation(value = "获取企业门禁信息详细信息")
	@GetMapping(value = "/{id}")
	public AjaxResults<PcFactoryAccessControl> getInfo(@PathVariable("id") String id) {
		return AjaxResults.success(pcFactoryAccessControlService.getById(id));
	}

	/**
	 * 新增企业门禁信息
	 */
	@ApiOperation(value = "新增企业门禁信息")
	@Log(title = "企业门禁信息", businessType = BusinessType.INSERT)
	@PostMapping(value = "/add")
	public AjaxResults<Boolean> add(@RequestBody PcFactoryAccessControlVo pcFactoryAccessControlVo) {
		PcFactoryAccessControl pcFactoryAccessControl = new PcFactoryAccessControl();
		BeanUtils.copyBeanProp(pcFactoryAccessControlVo, pcFactoryAccessControl);
		return pcFactoryAccessControlService.save(pcFactoryAccessControl) ? AjaxResults.success() : AjaxResults.error(300001, "新增失败");
	}


	/**
	 * 删除企业门禁信息
	 */
	@ApiOperation(value = "删除企业门禁信息")
	// @RequiresPermissions("talroad:pcFactoryAccessControl:remove")
	@Log(title = "企业门禁信息", businessType = BusinessType.DELETE)
	@PostMapping("/{ids}")
	public AjaxResults<Boolean> remove(@PathVariable String[] ids) {
		return pcFactoryAccessControlService.removeByIds(Arrays.asList(ids)) ? AjaxResults.success() : 
			AjaxResults.error(300004, "删除失败");
	}
}
