package com.ruoyi.web.controller.system;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.ss.store.service.IStoreAssembler;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.ss.store.domain.Store;
import com.ruoyi.ss.store.domain.StoreVO;
import com.ruoyi.ss.store.domain.StoreQuery;
import com.ruoyi.ss.store.service.IStoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 店铺Controller
 *
 * @author qzz
 * @date 2024-11-20
 */
@RestController
@RequestMapping("/system/store")
public class StoreController extends BaseController
{
    @Autowired
    private IStoreService storeService;

    @Autowired
    private IStoreAssembler storeAssembler;

    /**
     * 查询店铺列表
     */
    @PreAuthorize("@ss.hasPermi('system:store:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreQuery query)
    {
        startPage();
        startOrderBy();
        List<StoreVO> list = storeService.selectEStoreList(query);
        return getDataTable(list);
    }

    /**
     * 导出店铺列表
     */
    @PreAuthorize("@ss.hasPermi('system:store:export')")
    @Log(title = "店铺", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, StoreQuery query)
    {
        List<StoreVO> list = storeService.selectEStoreList(query);
        ExcelUtil<StoreVO> util = new ExcelUtil<StoreVO>(StoreVO.class);
        util.exportExcel(response, list, "店铺数据");
    }

    /**
     * 获取店铺详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:store:query')")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId, StoreQuery query)
    {
        StoreVO vo = storeService.selectEStoreByStoreId(storeId);
        // 拼接今日收入、本月收入、总设备数、总营收、房间总数、正在使用中的房间数、设施总数、正在使用中的设施数  统计数据
        storeAssembler.assembleStatisticalData(vo,query.getStartTime(),query.getEndTime());
        return success(vo);
    }

    /**
     * 新增店铺
     */
    @PreAuthorize("@ss.hasPermi('system:store:add')")
    @Log(title = "店铺", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Store eStore)
    {
        return toAjax(storeService.insertEStore(eStore));
    }

    /**
     * 修改店铺
     */
    @PreAuthorize("@ss.hasPermi('system:store:edit')")
    @Log(title = "店铺", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Store eStore)
    {
        return toAjax(storeService.updateEStore(eStore));
    }

    /**
     * 删除店铺
     */
    @PreAuthorize("@ss.hasPermi('system:store:remove')")
    @Log(title = "店铺", businessType = BusinessType.DELETE)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        return toAjax(storeService.deleteEStoreByStoreIds(storeIds));
    }

    /**
     * 开大门（管理员）
     */
    @GetMapping(value = "/openGate/{storeId}")
    public AjaxResult openGate(@PathVariable("storeId") Long storeId)
    {
        logger.info("开大门：【storeId={}】", storeId);
        boolean b = storeService.openGate(storeId, "管理员开大门",false);
        if(b){
            return success();
        }else{
            return error("",null);
        }
    }
}
