package com.qs.mp.web.controller.api.admin;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.qs.mp.admin.domain.ChannelApply;
import com.qs.mp.admin.domain.Supplier;
import com.qs.mp.admin.service.IChannelApplyService;
import com.qs.mp.common.core.domain.AjaxResult;
import com.qs.mp.common.core.page.TableDataInfo;
import com.qs.mp.common.utils.StringUtils;
import com.qs.mp.web.controller.common.BaseApiController;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.stereotype.Component;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author Evan
 * @date 2023/3/15
 */
@Api(tags = "创客申请管理API")
@RestController
@RequestMapping("/api/v1/mp/admin/channel/apply")
public class ChannelApplyController extends BaseApiController {

    @Autowired
    public IChannelApplyService channelApplyService;

    @ApiOperation("提交申请")
    @PostMapping("/submit")
    public AjaxResult submit(@RequestBody ChannelApply channelApply) {
        if (StringUtils.isEmpty(channelApply.getName())) {
            return AjaxResult.error("姓名不能为空");
        }
        if (StringUtils.isEmpty(channelApply.getPhone())) {
            return AjaxResult.error("手机号不能为空");
        }
        channelApplyService.save(channelApply);
        return AjaxResult.success();
    }

    @ApiOperation("列表")
    @PostMapping("/list")
    @ApiResponses(
        @ApiResponse(code = 200, message = "创客申请列表", response = ChannelApply.class)
    )
    @PreAuthorize("@ss.hasPermi('business:apply:list')")
    public TableDataInfo list(@RequestBody ChannelApply channelApply) {
        startPage();
        List<ChannelApply> list = channelApplyService.list(new LambdaQueryWrapper<ChannelApply>()
            .like(StringUtils.isNotBlank(channelApply.getName()), ChannelApply::getName, channelApply.getName())
            .eq(StringUtils.isNotBlank(channelApply.getPhone()), ChannelApply::getPhone, channelApply.getPhone())
            .eq(channelApply.getStatus() != null, ChannelApply::getStatus, channelApply.getStatus())
            .orderByAsc(ChannelApply::getStatus)
            .orderByDesc(ChannelApply::getId));
        return getDataTable(list);
    }

    @ApiOperation("更新为已联系")
    @PostMapping("/update/{id}")
    @ApiImplicitParams(
        @ApiImplicitParam(name = "id", value = "创客申请id", required = true, paramType = "path", dataType = "long")
    )
    @PreAuthorize("@ss.hasPermi('business:apply:update')")
    public AjaxResult update(@PathVariable("id") Long id) {
        channelApplyService.update(new LambdaUpdateWrapper<ChannelApply>()
            .set(ChannelApply::getStatus, 1)
            .eq(ChannelApply::getId, id));
        return AjaxResult.success();
    }
}
