
package com.tmerclub.cloud.product.controller.platform;

import com.tmerclub.cloud.api.product.dto.CategoryShopDTO;
import com.tmerclub.cloud.api.product.dto.HandleCategoryShopDTO;
import com.tmerclub.cloud.common.i18n.I18nMessage;
import com.tmerclub.cloud.common.response.ServerResponseEntity;
import com.tmerclub.cloud.common.security.AuthUserContext;
import com.tmerclub.cloud.common.util.ExcelUtil;
import com.tmerclub.cloud.product.service.CategoryService;
import com.tmerclub.cloud.product.service.CategoryShopService;
import com.tmerclub.cloud.product.vo.CategoryPlatformExcelVO;
import com.tmerclub.cloud.product.vo.CategoryShopVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

/**
 * @Author lth
 * @Date 2021/4/26 9:22
 */
@RestController("platformCategoryController")
@RequestMapping("/p/category")
@Tag(name = "platform-分类信息")
public class CategoryController {

    @Autowired
    private CategoryShopService categoryShopService;
    @Autowired
    private CategoryService categoryService;

    @GetMapping("/signing_info_by_shopId")
    @Operation(summary = "根据店铺id获取分类签约信息", description = "根据店铺id获取分类签约信息")
    @Parameter(name = "sysType", description = "系统类型 0.普通用户系统 1.商家端 2.平台端 3.供应商端")
    public ServerResponseEntity<List<CategoryShopVO>> listByShopId(@RequestParam("shopId") Long shopId, @RequestParam(value = "sysType", required = false) Integer sysType) {
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType() : sysType;
        List<CategoryShopVO> categoryShopVOList = categoryShopService.listByShopId(shopId, sysType, I18nMessage.getLang());
        return ServerResponseEntity.success(categoryShopVOList);
    }


    @PutMapping("/signing")
    @Operation(summary = "更新店铺签约分类", description = "更新店铺签约分类")
    public ServerResponseEntity<Void> signing(@Valid @RequestBody HandleCategoryShopDTO handleCategoryShopDTO) {
        List<CategoryShopDTO> categoryShopDTOList = handleCategoryShopDTO.getCategoryShopDTOList();
        Integer sysType = handleCategoryShopDTO.getSysType();
        Long shopId = handleCategoryShopDTO.getShopId();
        sysType = Objects.isNull(sysType) ? AuthUserContext.get().getSysType() : sysType;
        categoryShopService.signingCategory(categoryShopDTOList, shopId, sysType);
        return ServerResponseEntity.success();
    }

    @GetMapping("/soldExcel")
    @Operation(summary = "导出excel", description = "导出分类excel")
    public ServerResponseEntity<Void> spuSoldExcel(HttpServletResponse response) {
        List<CategoryPlatformExcelVO> list = categoryService.listExcelPlatform();
        ExcelUtil.soleExcel(response, list, CategoryPlatformExcelVO.EXCEL_NAME, CategoryPlatformExcelVO.MERGE_ROW_INDEX, CategoryPlatformExcelVO.MERGE_COLUMN_INDEX, CategoryPlatformExcelVO.class);
        return ServerResponseEntity.success();
    }
}
