package co.yixiang.yshop.module.shop.controller.admin.shopassistant;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.framework.excel.core.util.ExcelUtils;
import co.yixiang.yshop.framework.operatelog.core.annotations.OperateLog;
import co.yixiang.yshop.module.shop.controller.admin.shopassistant.vo.*;
import co.yixiang.yshop.module.shop.convert.shopassistant.ShopAssistantConvert;
import co.yixiang.yshop.module.shop.dal.dataobject.shopassistant.ShopAssistantDO;
import co.yixiang.yshop.module.shop.service.shopassistant.ShopAssistantService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;
import java.io.IOException;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;
import static co.yixiang.yshop.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 门店店员")
@RestController
@RequestMapping("/mall/shop-assistant")
@Validated
public class ShopAssistantController {

    @Resource
    private ShopAssistantService shopAssistantService;

    @PostMapping("/create")
    @Operation(summary = "创建门店店员")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:create')")
    public CommonResult<Long> createShopAssistant(@Valid @RequestBody ShopAssistantCreateReqVO createReqVO) {
        return success(shopAssistantService.createShopAssistant(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新门店店员")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:update')")
    public CommonResult<Boolean> updateShopAssistant(@Valid @RequestBody ShopAssistantUpdateReqVO updateReqVO) {
        shopAssistantService.updateShopAssistant(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除门店店员")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:delete')")
    public CommonResult<Boolean> deleteShopAssistant(@RequestParam("id") Long id) {
        shopAssistantService.deleteShopAssistant(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得门店店员")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:query')")
    public CommonResult<ShopAssistantRespVO> getShopAssistant(@RequestParam("id") Long id) {
        ShopAssistantDO shopAssistant = shopAssistantService.getShopAssistant(id);
        return success(ShopAssistantConvert.INSTANCE.convert(shopAssistant));
    }

    @GetMapping("/list")
    @Operation(summary = "获得门店店员列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:query')")
    public CommonResult<List<ShopAssistantRespVO>> getShopAssistantList(@RequestParam("ids") Collection<Long> ids) {
        List<ShopAssistantDO> list = shopAssistantService.getShopAssistantList(ids);
        return success(ShopAssistantConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得门店店员分页")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:query')")
    public CommonResult<PageResult<ShopAssistantRespVO>> getShopAssistantPage(@Valid ShopAssistantPageReqVO pageVO) {
        PageResult<ShopAssistantRespVO> pageResult = shopAssistantService.getShopAssistantPage(pageVO);
        return success(pageResult);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出门店店员 Excel")
    @PreAuthorize("@ss.hasPermission('mall:shop-assistant:export')")
    @OperateLog(type = EXPORT)
    public void exportShopAssistantExcel(@Valid ShopAssistantExportReqVO exportReqVO,
                                         HttpServletResponse response) throws IOException {
        List<ShopAssistantDO> list = shopAssistantService.getShopAssistantList(exportReqVO);
        // 导出 Excel
        List<ShopAssistantExcelVO> datas = ShopAssistantConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "门店店员.xls", "数据", ShopAssistantExcelVO.class, datas);
    }

}
