package com.jsowell.web.controller.pile;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Log;
import com.jsowell.common.core.controller.BaseController;
import com.jsowell.common.core.domain.AjaxResult;
import com.jsowell.common.core.page.TableDataInfo;
import com.jsowell.common.enums.BusinessType;
import com.jsowell.common.util.StringUtils;
import com.jsowell.common.util.poi.ExcelUtil;
import com.jsowell.pile.domain.PileBasicInfo;
import com.jsowell.pile.dto.BatchCreatePileDTO;
import com.jsowell.pile.dto.QueryPileDTO;
import com.jsowell.pile.dto.ReplaceMerchantStationDTO;
import com.jsowell.pile.service.IPileBasicInfoService;
import com.jsowell.pile.service.IPileMsgRecordService;
import com.jsowell.pile.vo.web.PileDetailVO;
import com.jsowell.service.PileService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 设备管理Controller
 *
 * @author jsowell
 * @date 2022-08-26
 */
@RestController
@RequestMapping("/pile/basic")
public class PileBasicInfoController extends BaseController {
	@Autowired
	private IPileBasicInfoService pileBasicInfoService;

	@Autowired
	private PileService pileService;

	@Autowired
	private IPileMsgRecordService pileMsgRecordService;
	/**
	 * 查询设备管理列表
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:list')")
	@GetMapping("/list")
	public TableDataInfo list(QueryPileDTO queryPileDTO) {
		// startPage(); // 在方法中分页
		List<PileDetailVO> list = pileBasicInfoService.queryPileInfos(queryPileDTO);
		return getDataTable(list);
	}

	/**
	 * 导出设备管理列表
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:export')")
	@Log(title = "设备管理", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, PileBasicInfo pileBasicInfo) {
		List<PileBasicInfo> list = pileBasicInfoService.selectPileBasicInfoList(pileBasicInfo);
		ExcelUtil<PileBasicInfo> util = new ExcelUtil<PileBasicInfo>(PileBasicInfo.class);
		util.exportExcel(response, list, "设备管理数据");
	}

	/**
	 * 获取设备管理详细信息
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(pileBasicInfoService.selectPileBasicInfoById(id));
	}

	/**
	 * 新增设备管理
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:add')")
	@Log(title = "设备管理", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody PileBasicInfo pileBasicInfo) {
		return toAjax(pileBasicInfoService.insertPileBasicInfo(pileBasicInfo));
	}

	/**
	 * 批量生成充电桩
	 * @param dto
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:add')")
	@Log(title = "设备管理", businessType = BusinessType.INSERT)
	@PostMapping("/batchAdd")
	public AjaxResult batchAdd(@RequestBody BatchCreatePileDTO dto) {
		logger.info("批量生成充电桩 param:{}", JSONObject.toJSONString(dto));
		return toAjax(pileService.batchCreatePile(dto));
	}

	/**
	 * 修改设备管理
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:edit')")
	@Log(title = "设备管理", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody PileBasicInfo pileBasicInfo) {
		return toAjax(pileBasicInfoService.updatePileBasicInfo(pileBasicInfo));
	}

	/**
	 * 删除设备管理
	 */
	@PreAuthorize("@ss.hasPermi('pile:basic:remove')")
	@Log(title = "设备管理", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(pileBasicInfoService.deletePileBasicInfoByIds(ids));
	}

	/**
	 * 批量更新充电桩
	 * 运营商 站点
	 */
	@PostMapping("/batchUpdatePileList")
	public AjaxResult batchUpdatePileList( @RequestBody ReplaceMerchantStationDTO dto) {
		logger.info("批量更新充电桩 param:{}", JSONObject.toJSONString(dto));
		return toAjax(pileBasicInfoService.replaceMerchantStationByPileIds(dto));
	}

	/**
	 * 查询充电桩详情
	 * @return
	 */
	@PostMapping("/getPileDetailById")
	public AjaxResult getPileDetailById(@RequestBody QueryPileDTO queryPileDTO) {
		logger.info("查询充电桩详情 param:{}", JSONObject.toJSONString(queryPileDTO));
		if (StringUtils.isBlank(queryPileDTO.getPileId())) {
			return AjaxResult.error("充电桩id不能为空");
		}
		return AjaxResult.success(pileBasicInfoService.selectBasicInfoById(Long.valueOf(queryPileDTO.getPileId())));
	}

	/**
	 * 查询充电桩通讯日志
	 *
	 * @param dto
	 * @return
	 */
	@PostMapping("/getPileFeedList")
	public AjaxResult getPileFeedList(@RequestBody QueryPileDTO dto) {
		// logger.info("查询充电桩通信日志 param:{}", dto.getPileSn());
		if (StringUtils.isBlank(dto.getPileSn())) {
			return AjaxResult.error("充电桩Sn不能为空");
		}
		return AjaxResult.success(pileMsgRecordService.getPileFeedList(dto));
	}

}
