package com.ruoyi.web.controller.ss;

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

import com.ruoyi.common.core.domain.ValidGroup;
import com.ruoyi.common.utils.ServiceUtil;
import com.ruoyi.ss.store.domain.StoreBO;
import com.ruoyi.ss.store.domain.StoreQuery;
import com.ruoyi.ss.store.domain.StoreVo;
import com.ruoyi.ss.store.service.StoreAssembler;
import com.ruoyi.ss.store.service.StoreValidator;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
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.service.StoreService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 商户Controller
 *
 * @author 邱贞招
 * @date 2024-02-21
 */
@RestController
@RequestMapping("/ss/store")
public class StoreController extends BaseController
{
    @Autowired
    private StoreService storeService;

    @Autowired
    private StoreValidator storeValidator;

    @Autowired
    private StoreAssembler storeAssembler;

    /**
     * 查询商户列表
     */
    @PreAuthorize("@ss.hasPermi('ss.store:list')")
    @GetMapping("/list")
    public TableDataInfo list(StoreQuery store)
    {
        startPage();
        List<StoreVo> list = storeService.selectSmStoreList(store);
        storeAssembler.assembleDeviceCount(list);
        storeAssembler.assembleRevenue(list);
        return getDataTable(list);
    }

    /**
     * ids查询商户列表
     */
    @PreAuthorize("@ss.hasPermi('ss.store:list')")
    @GetMapping("/listByIds/{ids}")
    public AjaxResult listByIds(@PathVariable Long[] ids)
    {
        List<StoreVo> list = storeService.selectSmStoreListByIds(Arrays.asList(ids));
        return success(list);
    }

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

    /**
     * 获取商户详细信息
     */
    @PreAuthorize("@ss.hasPermi('ss.store:query')")
    @GetMapping(value = "/{storeId}")
    public AjaxResult getInfo(@PathVariable("storeId") Long storeId)
    {
        StoreVo store = storeService.selectSmStoreById(storeId);
        List<StoreVo> list = Collections.singletonList(store);
        storeAssembler.assembleDeviceCount(list);   // 设备数量
        storeAssembler.assembleRevenue(list);       // 经营数据
        return success(store);
    }

    /**
     * 新增商户
     */
    @PreAuthorize("@ss.hasPermi('ss.store:add')")
    @Log(title = "商户", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody @Validated(ValidGroup.Create.class) StoreBO store)
    {
        store = store.filterCreate();
        ServiceUtil.assertion(storeValidator.preCreate(store));
        return toAjax(storeService.insertSmStore(store));
    }

    /**
     * 修改商户
     */
    @PreAuthorize("@ss.hasPermi('ss.store:edit')")
    @Log(title = "商户", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody @Validated(ValidGroup.Update.class) StoreBO store)
    {
        store = store.filterUpdate();
        ServiceUtil.assertion(storeValidator.preUpdate(store));
        return toAjax(storeService.updateSmStore(store));
    }

    /**
     * 删除商户
     */
    @PreAuthorize("@ss.hasPermi('ss.store:remove')")
    @Log(title = "商户", businessType = BusinessType.DELETE)
	@DeleteMapping("/{storeIds}")
    public AjaxResult remove(@PathVariable Long[] storeIds)
    {
        List<Long> ids = Arrays.asList(storeIds);
        ServiceUtil.assertion(storeValidator.preLogicDel(ids));
        return toAjax(storeService.logicDel(ids));
    }
}
