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

import com.cyksj.dto.Result;
import com.cyksj.enums.GatewayResponse;
import com.cyksj.model.entity.AdvertisementPopularize;
import com.cyksj.model.views.AdvertisementTencentView;
import com.cyksj.service.advertisement.AdvertisementPopularizeService;
import com.ejlchina.searcher.BeanSearcher;
import com.ejlchina.searcher.SearchResult;
import com.ejlchina.searcher.param.Operator;
import com.ejlchina.searcher.util.MapBuilder;
import com.ejlchina.searcher.util.MapUtils;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author chan
 * @date 2023/5/16 11:06 AM
 */
@RestController
@RequestMapping("manage/advertisement/popularize")
@Slf4j
@RequiredArgsConstructor
public class CmsAdvertisementPopularizeController {

    private final HttpServletRequest request;

    private final BeanSearcher beanSearcher;

    private final AdvertisementPopularizeService advertisementPopularizeService;

    @GetMapping
    public Result<SearchResult<AdvertisementPopularize>> get() {
        SearchResult<AdvertisementPopularize> searchResult = beanSearcher.search(AdvertisementPopularize.class,
                MapUtils.flatBuilder(request.getParameterMap())
                        .build());
        return GatewayResponse.SUCCESS.newBuilder().toResult(searchResult);
    }

    @PostMapping
    public Result<AdvertisementPopularize> post(@RequestBody AdvertisementPopularize popularize) {
        advertisementPopularizeService.save(popularize);
        popularize.setUrl(popularize.getUrl() + "?fid=" + popularize.getId());
        advertisementPopularizeService.updateById(popularize);
        return GatewayResponse.SUCCESS.newBuilder().toResult(advertisementPopularizeService.getById(popularize.getId()));
    }

    @PutMapping
    public Result<String> put(@RequestBody AdvertisementPopularize popularize) {
        advertisementPopularizeService.updateById(popularize);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }

    @DeleteMapping("/{id}")
    public Result<String> del(@PathVariable Long id) {
        advertisementPopularizeService.removeById(id);
        return GatewayResponse.SUCCESS.newBuilder().toResult();
    }


    @GetMapping("/getDataIdList")
    public Result<SearchResult<AdvertisementTencentView>> get(@RequestParam(required = false) Long accountId,
                                                              @RequestParam(required = false) Long userActionSetId) throws Exception {

        MapBuilder params = MapUtils.builder()
                .field(AdvertisementTencentView::getAccountId, accountId)
                .field(AdvertisementTencentView::getUserActionSetId, userActionSetId)
                .orderBy(AdvertisementTencentView::getId, "desc");
        // 选择数据源查询数据

        params.field(AdvertisementTencentView::getName)
                .op(Operator.NotNull)
                .onlySelect(AdvertisementTencentView::getUserActionSetId, AdvertisementTencentView::getName);
        List<AdvertisementTencentView> list = beanSearcher.searchAll(AdvertisementTencentView.class, params.build());
        list.stream().forEach(advertiserTencentVo -> advertiserTencentVo.setName(advertiserTencentVo.getName() + "-" + advertiserTencentVo.getUserActionSetId()));
        return GatewayResponse.SUCCESS.newBuilder().toResult(new SearchResult<>(list));


    }


}
