package com.jsowell.web.controller.pile;

import com.alibaba.fastjson2.JSONObject;
import com.jsowell.common.annotation.Log;
import com.jsowell.common.constant.Constants;
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.poi.ExcelUtil;
import com.jsowell.pile.domain.PileBillingTemplate;
import com.jsowell.pile.dto.CreateOrUpdateBillingTemplateDTO;
import com.jsowell.pile.dto.ImportBillingTemplateDTO;
import com.jsowell.pile.dto.PublishBillingTemplateDTO;
import com.jsowell.pile.service.IPileBillingTemplateService;
import com.jsowell.pile.vo.web.BillingTemplateVO;
import com.jsowell.service.PileRemoteService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
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-09-20
 */
@Api(value = "计费模板Controller", tags = { "计费模板" })
@RestController
@RequestMapping("/billing/template")
public class PileBillingTemplateController extends BaseController {
	@Autowired
	private IPileBillingTemplateService pileBillingTemplateService;

	@Autowired
	private PileRemoteService pileRemoteService;

	/**
	 * 查询计费模板列表
	 */
	@PreAuthorize("@ss.hasPermi('billing:template:list')")
	@GetMapping("/list")
	public TableDataInfo list(PileBillingTemplate pileBillingTemplate) {
		startPage();
		// 只查询公共的
		pileBillingTemplate.setPublicFlag(Constants.ONE);
		List<PileBillingTemplate> list = pileBillingTemplateService.selectPileBillingTemplateList(pileBillingTemplate);
		return getDataTable(list);
	}

	/**
	 * 导出计费模板列表
	 */
	@PreAuthorize("@ss.hasPermi('billing:template:export')")
	@Log(title = "计费模板", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, PileBillingTemplate pileBillingTemplate) {
		List<PileBillingTemplate> list = pileBillingTemplateService.selectPileBillingTemplateList(pileBillingTemplate);
		ExcelUtil<PileBillingTemplate> util = new ExcelUtil<PileBillingTemplate>(PileBillingTemplate.class);
		util.exportExcel(response, list, "计费模板数据");
	}

	/**
	 * 获取计费模板详细信息
	 */
	@PreAuthorize("@ss.hasPermi('billing:template:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfoById(@PathVariable("id") Long id) {
		return AjaxResult.success(pileBillingTemplateService.queryPileBillingTemplateById(id));
	}

	/**
	 * 修改计费模板
	 */
	@PreAuthorize("@ss.hasPermi('billing:template:edit')")
	@Log(title = "计费模板", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody PileBillingTemplate pileBillingTemplate) {
		return toAjax(pileBillingTemplateService.updatePileBillingTemplate(pileBillingTemplate));
	}

	/**
	 * 删除计费模板
	 */
	@PreAuthorize("@ss.hasPermi('billing:template:remove')")
	@Log(title = "计费模板", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(pileBillingTemplateService.deletePileBillingTemplateByIds(ids));
	}

	/**
	 * 新增计费模板
	 * http://localhost:8080/billing/template/createBillingTemplate
	 */
	@ApiOperation("新增计费模板")
	@PreAuthorize("@ss.hasPermi('billing:template:add')")
	@Log(title = "计费模板", businessType = BusinessType.INSERT)
	@PostMapping("/createBillingTemplate")
	public AjaxResult createBillingTemplate(@RequestBody CreateOrUpdateBillingTemplateDTO dto) {
		logger.info("新增计费模板 param:{}", JSONObject.toJSONString(dto));
		pileBillingTemplateService.createBillingTemplate(dto);
		return AjaxResult.success();
	}

	/**
	 * 更新计费模板 前端
	 * @param dto
	 * @return
	 */
	@PostMapping("/updateBillingTemplate")
	public AjaxResult updateBillingTemplate(@RequestBody CreateOrUpdateBillingTemplateDTO dto) {
		logger.info("修改计费模板 param:{}", JSONObject.toJSONString(dto));
		pileBillingTemplateService.updateBillingTemplate(dto);
		return AjaxResult.success();
	}

	/**
	 * 查询公共计费模板
	 */
	@GetMapping("/queryPublicBillingTemplateList")
	public TableDataInfo queryPublicBillingTemplateList() {
		List<BillingTemplateVO> list = pileBillingTemplateService.queryPublicBillingTemplateList();
		return getDataTable(list);
	}

	/**
	 * 站点导入计费模板接口
	 * http://localhost:8080/billing/template/stationImportBillingTemplate
	 */
	@PostMapping("/stationImportBillingTemplate")
	public AjaxResult stationImportBillingTemplate(@RequestBody ImportBillingTemplateDTO dto) {
		logger.info("站点导入计费模板 param:{}", JSONObject.toJSONString(dto));
		return toAjax(pileBillingTemplateService.stationImportBillingTemplate(dto));
	}

	/**
	 * 查询站点计费模板
	 */
	@GetMapping("/queryStationBillingTemplateList/{stationId}")
	public TableDataInfo queryStationBillingTemplateList(@PathVariable("stationId") String stationId) {
		logger.info("查询站点计费模板 param:{}", stationId);
		List<BillingTemplateVO> list = pileBillingTemplateService.queryStationBillingTemplateList(stationId);
		logger.info("查询站点计费模板 result：{}", JSONObject.toJSONString(list));
		return getDataTable(list);
	}

	/**
	 * 发布计费模板
	 */
	@PostMapping("/publishBillingTemplate")
	public AjaxResult publishBillingTemplate(@RequestBody PublishBillingTemplateDTO dto) {
		return toAjax(pileRemoteService.publishBillingTemplate(dto));
	}
}
