package co.yixiang.yshop.module.distributor.controller.admin.distributor;

import co.yixiang.yshop.framework.common.pojo.CommonResult;
import co.yixiang.yshop.framework.common.pojo.PageResult;
import co.yixiang.yshop.module.distributor.controller.admin.distributor.vo.*;
import co.yixiang.yshop.module.distributor.convert.distributor.DistributorConvert;
import co.yixiang.yshop.module.distributor.dal.dataobject.distributor.DistributorDO;
import co.yixiang.yshop.module.distributor.service.distributor.DistributorFacade;
import co.yixiang.yshop.module.distributor.service.distributor.DistributorService;
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.validation.Valid;
import java.util.Collection;
import java.util.List;

import static co.yixiang.yshop.framework.common.pojo.CommonResult.success;

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

    @Resource
    private DistributorService distributorService;

    @Resource
    private DistributorFacade distributorFacade;

    @PutMapping("/update")
    @Operation(summary = "更新分销商")
    @PreAuthorize("@ss.hasPermission('distributor::update')")
    public CommonResult<Boolean> update(@Valid @RequestBody DistributorUpdateReqVO updateReqVO) {
        distributorService.update(updateReqVO);
        return success(true);
    }

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

    @GetMapping("/get")
    @Operation(summary = "获得分销商详情")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('distributor::query')")
    public CommonResult<DistributorRespVO> get(@RequestParam("id") Long id) {
        return success(distributorFacade.getDetails(id));
    }

    @GetMapping("/list")
    @Operation(summary = "获得分销商列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('distributor::query')")
    public CommonResult<List<DistributorRespVO>> getList(@RequestParam("ids") Collection<Long> ids) {
        List<DistributorDO> list = distributorService.getList(ids);
        return success(DistributorConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得分销商分页")
    @PreAuthorize("@ss.hasPermission('distributor::query')")
    public CommonResult<PageResult<DistributorRespVO>> getPage(@Valid DistributorPageReqVO pageVO) {
        PageResult<DistributorDO> pageResult = distributorFacade.getPage(pageVO);
        return success(DistributorConvert.INSTANCE.convertPage(pageResult));
    }

    @PostMapping("/verify")
    @Operation(summary = "审核分销商")
    @PreAuthorize("@ss.hasPermission('distributor::create')")
    public CommonResult<Boolean> verify(@Valid @RequestBody DistributorApplyReqVO reqVO) {
        distributorFacade.verify(reqVO);
        return success(Boolean.TRUE);
    }

    // 清退分销商
    @PostMapping("/clearance")
    @Operation(summary = "清退/恢复分销商")
    @PreAuthorize("@ss.hasPermission('distributor::create')")
    public CommonResult<Boolean> clearance(@Valid @RequestBody DistributorClearanceReqVO reqVO) {
        distributorFacade.clearance(reqVO);
        return success(Boolean.TRUE);
    }

    @PostMapping("/restore")
    @Operation(summary = "恢复分销商")
    @PreAuthorize("@ss.hasPermission('distributor::create')")
    public CommonResult<Boolean> restore(@Valid @RequestBody DistributorClearanceReqVO reqVO) {
        distributorFacade.restore(reqVO);
        return success(Boolean.TRUE);
    }

}
