package com.cyksj.web.controller.manage.corp;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.cyksj.common.EnvCommonService;
import com.cyksj.common.exception.BusinessRuntimeException;
import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.CorpCustomerAcquisitionLinkPopularize;
import com.cyksj.model.views.CorpCustomerAcquisitionLinkPopularizeView;
import com.cyksj.service.corp.CorpCustomerAcquisitionLinkPopularizeService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 *  获客助手
 * @author chan
 * @date 2023/11/10 09:53
 */
@RestController
@RequestMapping("/v2/corp/customer/acuistion/popularize")
@RequiredArgsConstructor
public class CorpCustomerAcquistionLinkPopularizeController {
    private final CorpCustomerAcquisitionLinkPopularizeService corpCustomerAcquisitionLinkPopularizeService;

    private final BeanSearcher beanSearcher;

    private final HttpServletRequest request;

    private final EnvCommonService envCommonService;

    /**
     * 创建加粉链接
     */
    @PostMapping
    public Result<String> add(@RequestBody CorpCustomerAcquisitionLinkPopularize corpCustomerAcquisitionLinkPopularize){
        CorpCustomerAcquisitionLinkPopularize linkPopularize = corpCustomerAcquisitionLinkPopularizeService.add(corpCustomerAcquisitionLinkPopularize);
        StringBuilder url = new StringBuilder();
        url.append(envCommonService.getNotifyHost()).append("applets/channel/customerAcquistion?projectid=__PROJECT_ID__&clickid=__CLICKID__&linkId=").append(linkPopularize.getId());
        return GatewayResponse.SUCCESS.newBuilder().toResult(url.toString());
    }

    /**
     * 获取加粉链接
     * @param id 推广链接id
     * @return 加粉链接
     */
    @GetMapping("/{id}")
    public Result<CorpCustomerAcquisitionLinkPopularize> get(@PathVariable Long id){
        CorpCustomerAcquisitionLinkPopularize linkPopularize = corpCustomerAcquisitionLinkPopularizeService.
                getOne(Wrappers.lambdaQuery(CorpCustomerAcquisitionLinkPopularize.class)
                        .eq(CorpCustomerAcquisitionLinkPopularize::getPopularizeId, id)
                        .last("limit 1"));
        if (linkPopularize == null) {
            throw BusinessRuntimeException.getInstance("请先创建加粉链接。");
        }

        StringBuilder url = new StringBuilder();
        url.append(envCommonService.getNotifyHost()).append("applets/channel/customerAcquistion?projectid=__PROJECT_ID__&clickid=__CLICKID__&linkId=").append(linkPopularize.getId());

        linkPopularize.setUrl(url.toString());
        return GatewayResponse.SUCCESS.newBuilder().toResult(linkPopularize);
    }


    /**
     * 编辑加粉链接
     */
    @PutMapping
    public Result<String> up(@RequestBody CorpCustomerAcquisitionLinkPopularize corpCustomerAcquisitionLinkPopularize){
        CorpCustomerAcquisitionLinkPopularize popularize = corpCustomerAcquisitionLinkPopularizeService.getById(corpCustomerAcquisitionLinkPopularize.getId());
        if (popularize  == null) {
            throw BusinessRuntimeException.getInstance("该加粉链接不存在.");
        }
        corpCustomerAcquisitionLinkPopularizeService.updateById(corpCustomerAcquisitionLinkPopularize);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    /**
     * 加粉链接列表
     */
    @GetMapping("/get")
    public Result<SearchResult<CorpCustomerAcquisitionLinkPopularizeView>> get(){
        SearchResult<CorpCustomerAcquisitionLinkPopularizeView> search = beanSearcher.search(CorpCustomerAcquisitionLinkPopularizeView.class, MapUtils.flatBuilder(request.getParameterMap()).build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(search);
    }


}
