package cn.iocoder.yudao.module.member.controller.app.storegoodstype;

import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;
import cn.iocoder.yudao.framework.operatelog.core.annotations.OperateLog;
import cn.iocoder.yudao.module.member.controller.admin.storegoodstype.vo.StoreGoodsTypePageReqVO;
import cn.iocoder.yudao.module.member.controller.admin.storegoodstype.vo.StoreGoodsTypeRespVO;
import cn.iocoder.yudao.module.member.controller.admin.storegoodstype.vo.StoreGoodsTypeSaveReqVO;
import cn.iocoder.yudao.module.member.dal.dataobject.storegoodstype.StoreGoodsTypeDO;
import cn.iocoder.yudao.module.member.service.storegoodstype.StoreGoodsTypeService;
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.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

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

import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;
import static cn.iocoder.yudao.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;

@Tag(name = "APP超市 - 商品类别")
@RestController
@RequestMapping("/member/store-goods-type")
@Validated
public class AppStoreGoodsTypeController {

    @Resource
    private StoreGoodsTypeService storeGoodsTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建商品类别")
    public CommonResult<Integer> createStoreGoodsType(@Valid @RequestBody StoreGoodsTypeSaveReqVO createReqVO) {
        return success(storeGoodsTypeService.createStoreGoodsType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新商品类别")
    public CommonResult<Boolean> updateStoreGoodsType(@Valid @RequestBody StoreGoodsTypeSaveReqVO updateReqVO) {
        storeGoodsTypeService.updateStoreGoodsType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除商品类别")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteStoreGoodsType(@RequestParam("id") Integer id) {
        storeGoodsTypeService.deleteStoreGoodsType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得商品类别")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<StoreGoodsTypeRespVO> getStoreGoodsType(@RequestParam("id") Integer id) {
        StoreGoodsTypeDO storeGoodsType = storeGoodsTypeService.getStoreGoodsType(id);
        return success(BeanUtils.toBean(storeGoodsType, StoreGoodsTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得商品类别分页")
    public CommonResult<PageResult<StoreGoodsTypeRespVO>> getStoreGoodsTypePage(@Valid StoreGoodsTypePageReqVO pageReqVO) {
        PageResult<StoreGoodsTypeDO> pageResult = storeGoodsTypeService.getStoreGoodsTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, StoreGoodsTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出商品类别 Excel")
    @OperateLog(type = EXPORT)
    public void exportStoreGoodsTypeExcel(@Valid StoreGoodsTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<StoreGoodsTypeDO> list = storeGoodsTypeService.getStoreGoodsTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "商品类别.xls", "数据", StoreGoodsTypeRespVO.class,
                        BeanUtils.toBean(list, StoreGoodsTypeRespVO.class));
    }

    @GetMapping("/all")
    @Operation(summary = "获得所有商品类别")
    public CommonResult<List<StoreGoodsTypeRespVO>> getAll() {
        List<StoreGoodsTypeDO> all = storeGoodsTypeService.getAll();
        return success(BeanUtils.toBean(all, StoreGoodsTypeRespVO.class));
    }

}