package com.jsowell.web.controller.pile;

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.exception.BusinessException;
import com.jsowell.common.util.PageUtils;
import com.jsowell.common.util.poi.ExcelUtil;
import com.jsowell.pile.domain.PileMerchantInfo;
import com.jsowell.pile.dto.CreateMerchantDTO;
import com.jsowell.pile.dto.QueryMerchantInfoDTO;
import com.jsowell.pile.service.IPileMerchantInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

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

/**
 * 充电桩运营商信息Controller
 *
 * @author jsowell
 * @date 2022-08-27
 */
@RestController
@RequestMapping("/pile/merchant")
public class PileMerchantInfoController extends BaseController {
	@Autowired
	private IPileMerchantInfoService pileMerchantInfoService;

	/**
	 * 查询充电桩运营商信息列表
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:list')")
	@GetMapping("/list")
	public TableDataInfo list(PileMerchantInfo pileMerchantInfo) {
		startPage();
		List<PileMerchantInfo> list = pileMerchantInfoService.selectPileMerchantInfoListWithAuth(pileMerchantInfo);
		return getDataTable(list);
	}


	/**
	 * 查询一级运营商列表
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:list')")
	@GetMapping("queryFirstLevelMerchant")
	public TableDataInfo queryFirstLevelMerchant() {
		List<PileMerchantInfo> list = pileMerchantInfoService.queryFirstLevelMerchantList();
		return getDataTable(list);
	}

	/**
	 * 获取运营商列表 不分页
	 * @param pileMerchantInfo
	 * @return
	 */
	// @PreAuthorize("@ss.hasPermi('pile:merchant:list')")
	@GetMapping("/getMerchantList")
	public TableDataInfo getMerchantList(PileMerchantInfo pileMerchantInfo) {
		List<PileMerchantInfo> list = pileMerchantInfoService.selectPileMerchantInfoListWithAuth(pileMerchantInfo);
		return getDataTable(list);
	}

	/**
	 * 导出充电桩运营商信息列表
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:export')")
	@Log(title = "充电桩运营商信息", businessType = BusinessType.EXPORT)
	@PostMapping("/export")
	public void export(HttpServletResponse response, PileMerchantInfo pileMerchantInfo) {
		List<PileMerchantInfo> list = pileMerchantInfoService.selectPileMerchantInfoListWithAuth(pileMerchantInfo);
		ExcelUtil<PileMerchantInfo> util = new ExcelUtil<PileMerchantInfo>(PileMerchantInfo.class);
		util.exportExcel(response, list, "充电桩运营商信息数据");
	}

	/**
	 * 获取充电桩运营商信息详细信息
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:query')")
	@GetMapping(value = "/{id}")
	public AjaxResult getInfo(@PathVariable("id") Long id) {
		return AjaxResult.success(pileMerchantInfoService.selectPileMerchantInfoById(id));
	}

	/**
	 * 新增充电桩运营商信息
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:add')")
	@Log(title = "充电桩运营商信息", businessType = BusinessType.INSERT)
	@PostMapping
	public AjaxResult add(@RequestBody CreateMerchantDTO pileMerchantInfo) {
		AjaxResult result;
		try {
			int i = pileMerchantInfoService.insertPileMerchantInfo(pileMerchantInfo);
			result = toAjax(i);
		} catch (BusinessException e) {
			result = AjaxResult.error(e.getMessage());
		}
		return result;
	}

	/**
	 * 修改充电桩运营商信息
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:edit')")
	@Log(title = "充电桩运营商信息", businessType = BusinessType.UPDATE)
	@PutMapping
	public AjaxResult edit(@RequestBody PileMerchantInfo pileMerchantInfo) {
		return toAjax(pileMerchantInfoService.updatePileMerchantInfo(pileMerchantInfo));
	}

	/**
	 * 删除充电桩运营商信息
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:remove')")
	@Log(title = "充电桩运营商信息", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
	public AjaxResult remove(@PathVariable Long[] ids) {
		return toAjax(pileMerchantInfoService.deletePileMerchantInfoByIds(ids));
	}

	/**
	 * 财务管理页面获取运营商列表
	 * @return
	 */
	@PreAuthorize("@ss.hasPermi('pile:merchant:query')")
	@PostMapping("/getFinancialMerchantList")
	public TableDataInfo getFinancialMerchantList(@RequestBody QueryMerchantInfoDTO dto) {
		int pageNum = dto.getPageNum() == 0 ? 1 : dto.getPageNum();
		int pageSize = dto.getPageSize() == 0 ? 10 : dto.getPageSize();
		PageUtils.startPage(pageNum, pageSize);
		return getDataTable(pileMerchantInfoService.queryMerchantInfoListByAuth(dto));
	}
}
