package com.storlead.trade.controller;

import com.storlead.trade.dto.MarketingCampaignDTO;
import com.storlead.trade.service.MarketingCampaignEntityService;
import com.storlead.framework.common.result.Result;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
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;

import java.util.List;

@RestController
@RequestMapping("/marketing")
@Api(tags = "营销: 营销活动")
public class MarketingController {

    @Autowired
    private MarketingCampaignEntityService marketingService;
    @PostMapping("/list")
    @ApiOperation("营销: 营销活动列表")
    public Result<Object> list(@RequestBody MarketingCampaignDTO marketingCampaignDTO) {

        Result<Object> result = marketingService.getList(marketingCampaignDTO);
        return Result.result(result);
    }

    @PostMapping(value = "/add")
    @ApiOperation(value = "营销: 营销活动添加", notes = "营销: 营销活动添加简报")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> add(@RequestBody MarketingCampaignDTO marketingCampaignDTO) {
        Result<Object> result = marketingService.addMarket(marketingCampaignDTO);
        return Result.result(result);
    }

    @PostMapping(value = "/edit")
    @ApiOperation(value = "营销: 营销活动修改", notes = "营销: 营销活动修改")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> edit(@RequestBody MarketingCampaignDTO marketingCampaignDTO) {
        Result<Object> result = marketingService.editMarket(marketingCampaignDTO);
        return Result.result(result);
    }

    @PostMapping(value = "/delete")
    @ApiOperation(value = "营销: 营销活动修改", notes = "营销: 营销活动修改")
    @Transactional(rollbackFor = Throwable.class)
    public Result<?> delete(@RequestBody MarketingCampaignDTO marketingCampaignDTO) {
        boolean isOk = false;
        Long id = marketingCampaignDTO.getId();
        if (!ObjectUtils.isEmpty(id)) {
            isOk = marketingService.removeById(id);
        }
        List<Long> idList = marketingCampaignDTO.getIdList();
        if (!CollectionUtils.isEmpty(idList)){
            isOk = marketingService.removeByIds(idList);
        }
        if (!isOk){
            return Result.error("删除失败");
        }
        return Result.ok();
    }

}
