package com.yc.ship.module.ota.controller.admin.distributortype;

import com.yc.ship.framework.common.pojo.CommonResult;
import com.yc.ship.framework.common.pojo.PageParam;
import com.yc.ship.framework.common.pojo.PageResult;
import com.yc.ship.framework.common.util.object.BeanUtils;
import com.yc.ship.framework.excel.core.util.ExcelUtils;
import com.yc.ship.framework.operatelog.core.annotations.OperateLog;
import com.yc.ship.module.ota.controller.admin.distributortype.vo.DistributorTypePageReqVO;
import com.yc.ship.module.ota.controller.admin.distributortype.vo.DistributorTypeRespVO;
import com.yc.ship.module.ota.controller.admin.distributortype.vo.DistributorTypeSaveReqVO;
import com.yc.ship.module.ota.dal.dataobject.distributortype.DistributorTypeDO;
import com.yc.ship.module.ota.service.distributortype.DistributorTypeService;
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.List;
import java.util.Map;

import static com.yc.ship.framework.common.pojo.CommonResult.success;
import static com.yc.ship.framework.operatelog.core.enums.OperateTypeEnum.EXPORT;


@Tag(name = "管理后台 - 分销商类型")
@RestController
@RequestMapping("/distributor-type")
@Validated
public class DistributorTypeController {

    @Resource
    private DistributorTypeService distributorTypeService;

    @PostMapping("/create")
    @Operation(summary = "创建分销商类型")
    @PreAuthorize("@ss.hasPermission('ota:distributor-type:create')")
    public CommonResult<Long> createDistributorType(@Valid @RequestBody DistributorTypeSaveReqVO createReqVO) {
        return success(distributorTypeService.createDistributorType(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新分销商类型")
    @PreAuthorize("@ss.hasPermission('ota:distributor-type:update')")
    public CommonResult<Boolean> updateDistributorType(@Valid @RequestBody DistributorTypeSaveReqVO updateReqVO) {
        distributorTypeService.updateDistributorType(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除分销商类型")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('ota:distributor-type:delete')")
    public CommonResult<Boolean> deleteDistributorType(@RequestParam("id") Long id) {
        distributorTypeService.deleteDistributorType(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得分销商类型")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-type:query')")
    public CommonResult<DistributorTypeRespVO> getDistributorType(@RequestParam("id") Long id) {
        DistributorTypeDO distributorType = distributorTypeService.getDistributorType(id);
        return success(BeanUtils.toBean(distributorType, DistributorTypeRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商类型分页")
//    @PreAuthorize("@ss.hasPermission('ota:distributor-type:query')")
    public CommonResult<PageResult<DistributorTypeRespVO>> getDistributorTypePage(@Valid DistributorTypePageReqVO pageReqVO) {
        PageResult<DistributorTypeDO> pageResult = distributorTypeService.getDistributorTypePage(pageReqVO);
        return success(BeanUtils.toBean(pageResult, DistributorTypeRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出分销商类型 Excel")
    @PreAuthorize("@ss.hasPermission('ota:distributor-type:export')")
    @OperateLog(type = EXPORT,enable = false)
    public void exportDistributorTypeExcel(@Valid DistributorTypePageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<DistributorTypeDO> list = distributorTypeService.getDistributorTypePage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "分销商类型.xls", "数据", DistributorTypeRespVO.class,
                        BeanUtils.toBean(list, DistributorTypeRespVO.class));
    }

    @PutMapping("/update-status")
    @Operation(summary = "更改状态")
    @PreAuthorize("@ss.hasPermission('ota:distributor-type:update')")
    public CommonResult<Boolean> updateStatus(@Valid @RequestBody DistributorTypeSaveReqVO updateReqVO) {
        distributorTypeService.updateStatus(updateReqVO.getId(),updateReqVO.getIsUse());
        return success(true);
    }

    @GetMapping({"/list-all-simple", "/simple-list"})
    @Operation(summary = "获取分销商类型精简信息列表", description = "只包含被开启的类型，主要用于前端的下拉选项")
    public CommonResult<List<Map>> getSimpleList() {
        List<Map> list= distributorTypeService.getSimpleList();
        return success(list);
    }
}