package com.hwrj.cloud.admin.controller;

import com.hwrj.cloud.admin.model.UmsCompanyShare;
import com.hwrj.cloud.admin.service.UmsCompanyShareService;
import com.hwrj.cloud.common.api.CommonPage;
import com.hwrj.cloud.common.api.CommonResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Slf4j
@Api(tags = "UmsCompanyShareController",description = "供应商分享码id")
@RestController
@RequestMapping("/comp/share")
public class UmsCompanyShareController {
    @Autowired
    private UmsCompanyShareService umsCompanyShareService;

    @ApiOperation(value = "分享码生成")
    @GetMapping("/add")
    public CommonResult shareCodeAdd(@RequestParam(required = true,value = "compId")Long compId, @RequestParam(required = true,value = "compName")String compName){
        String s = umsCompanyShareService.addShare(compId, compName);
        if (StringUtils.isEmpty(s)){
            return CommonResult.failed("邀请码生成失败");
        }
        return CommonResult.success();
    }

    @ApiOperation(value = "获取供应商的分享码列表")
    @GetMapping("/list")
    public CommonResult shareCodeList(@RequestParam("compId")Long compId,
                                      @RequestParam(required = false,value = "status",defaultValue = "8")int status,
                                      @RequestParam(required = false,value = "pageNo",defaultValue = "0")int pageNo,
                                      @RequestParam(required = false,value = "pageSize",defaultValue = "10")int pageSize){
        List<UmsCompanyShare> shares = umsCompanyShareService.shareList(compId, status, pageNo, pageSize);
        if (shares == null||shares.size()<1){
            return CommonResult.success();
        }
        return CommonResult.success(CommonPage.restPage(shares));
    }

    @ApiOperation(value = "获取供应商的分享码列表")
    @GetMapping("/detail")
    public CommonResult shareCodeList(@RequestParam("id")Long id){
        UmsCompanyShare share = umsCompanyShareService.shareDetail(id);
        if (share == null){
            return CommonResult.failed("获取详情失败");
        }
        return CommonResult.success(share);
    }


    @ApiOperation(value = "删除供应商分享码")
    @GetMapping("/delete")
    public CommonResult shareCodeDelete(@RequestParam("id")Long id){
        umsCompanyShareService.shareUpdate(id,2);
        return CommonResult.success();
    }


}
