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.poi.ExcelUtil;
import com.jsowell.pile.domain.PileSimInfo;
import com.jsowell.pile.dto.QuerySimInfoDTO;
import com.jsowell.pile.dto.SimRenewDTO;
import com.jsowell.pile.service.IPileSimInfoService;
import com.jsowell.pile.service.SimCardService;
import com.jsowell.pile.vo.web.SimCardInfoVO;
import com.jsowell.pile.vo.web.SimRenewResultVO;
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;

/**
 * 充电桩SIM卡信息Controller
 *
 * @author jsowell
 * @date 2022-08-26
 */
@RestController
@RequestMapping("/pile/sim")
public class PileSimInfoController extends BaseController {
    @Autowired
    private IPileSimInfoService pileSimInfoService;

    @Autowired
    private SimCardService simCardService;

    /**
     * 查询充电桩SIM卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:list')")
    @GetMapping("/list")
    public TableDataInfo list(PileSimInfo pileSimInfo) {
        startPage();
        List<PileSimInfo> list = pileSimInfoService.selectPileSimInfoList(pileSimInfo);

        // List<SimCardInfoVO> list = pileSimInfoService.getSimInfoList(pileSimInfo);
        return getDataTable(list);
    }

    @PreAuthorize("@ss.hasPermi('pile:sim:list')")
    @PostMapping("/getSimInfo")
    public TableDataInfo getSimInfo(QuerySimInfoDTO dto) {
        startPage();
        List<SimCardInfoVO> simInfoList = pileSimInfoService.getSimInfoList(dto);
        return getDataTable(simInfoList);
    }

    /**
     * 导出充电桩SIM卡信息列表
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:export')")
    @Log(title = "充电桩SIM卡信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, QuerySimInfoDTO dto) {
        List<SimCardInfoVO> list = pileSimInfoService.getSimInfoList(dto);
        ExcelUtil<SimCardInfoVO> util = new ExcelUtil<>(SimCardInfoVO.class);
        util.exportExcel(response, list, "充电桩SIM卡信息数据");
    }

    /**
     * 获取充电桩SIM卡信息详细信息
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        return AjaxResult.success(pileSimInfoService.selectPileSimInfoById(id));
    }

    /**
     * 新增充电桩SIM卡信息
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:add')")
    @Log(title = "充电桩SIM卡信息", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PileSimInfo pileSimInfo) {
        return toAjax(pileSimInfoService.insertPileSimInfo(pileSimInfo));
    }

    /**
     * 修改充电桩SIM卡信息
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:edit')")
    @Log(title = "充电桩SIM卡信息", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PileSimInfo pileSimInfo) {
        return toAjax(pileSimInfoService.updatePileSimInfo(pileSimInfo));
    }

    /**
     * 删除充电桩SIM卡信息
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:remove')")
    @Log(title = "充电桩SIM卡信息", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids) {
        return toAjax(pileSimInfoService.deletePileSimInfoByIds(ids));
    }


    /**
     * 续费sim卡周期
     * @param dto
     * @return
     */
    @PreAuthorize("@ss.hasPermi('pile:sim:edit')")
    @PostMapping("/simRenew")
    public AjaxResult simRenew(@RequestBody SimRenewDTO dto) {
        List<SimRenewResultVO> list = simCardService.renewSimByLoop(dto.getIccIds(), dto.getCycleNumber());
        return AjaxResult.success(list);


        // AjaxResult result;
        // try {
        //     List<SimRenewResultVO> list = simCardService.renewSimByLoop(dto.getIccIds(), dto.getCycleNumber());
        //     result = AjaxResult.
        // } catch (BusinessException e) {
        //     result = AjaxResult.error(Integer.parseInt(e.getCode()), e.getMessage());
        // } catch (Exception e) {
        //     result = AjaxResult.error();
        // }
        // return result;
    }

}
