package com.neusoft.smart.pos.promotion.controller;

import com.neusoft.smart.pos.dto.promotion.request.GetPreferentialPriceRequest;
import com.neusoft.smart.pos.dto.promotion.request.PromotionPayChannelQueryRequest;
import com.neusoft.smart.pos.dto.promotion.request.PromotionPayChannelRequest;
import com.neusoft.smart.pos.dto.promotion.response.GetPreferentialPriceResponse;
import com.neusoft.smart.pos.dto.promotion.response.PromotionPayChannelResponse;
import com.neusoft.smart.pos.framework.bean.PaginationExtendBean;
import com.neusoft.smart.pos.framework.dto.ResponseData;
import com.neusoft.smart.pos.promotion.service.PromotionService;
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.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

@RestController("/promotion")
@Slf4j
@Api(value = "促销/营销相关restful接口", description = "促销/营销相关restful接口")
public class PromotionController {
    
    @Autowired
    private PromotionService promotionService;
    
    @ApiOperation(value = "计算优惠价格")
    @PostMapping("/preferential/price")
    public ResponseData<GetPreferentialPriceResponse> getDiscounts(@RequestBody GetPreferentialPriceRequest getPreferentialPriceRequest) {
        return new ResponseData<>(promotionService.getPreferentialPriceNew(getPreferentialPriceRequest));
    }
    
    @ApiOperation(value = "保存支付营销")
    @PostMapping("/payment/promotion")
    public ResponseData<?> createPaymentPromotion(@RequestBody @Valid PromotionPayChannelRequest promotionPayChannelRequest) {
        promotionService.create(promotionPayChannelRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "更新支付营销")
    @PutMapping("/payment/promotion")
    public ResponseData<?> updatePaymentPromotion(@RequestBody @Valid PromotionPayChannelRequest promotionPayChannelRequest) {
        promotionService.update(promotionPayChannelRequest);
        return new ResponseData<>();
    }
    
    @ApiOperation(value = "删除支付营销")
    @DeleteMapping("/payment/promotion/{id}")
    public ResponseData<?> deletePaymentPromotion(@PathVariable Integer id) {
        promotionService.delete(id);
        return new ResponseData<>();
    }
    
    @PostMapping("/payment/promotions")
    @ApiOperation(value = "分页查询支付营销接口", notes = "分页查询支付营销接口")
    public ResponseData<PaginationExtendBean<PromotionPayChannelResponse>> groupList(@RequestBody PromotionPayChannelQueryRequest promotionPayChannelQueryRequest) {
        PaginationExtendBean<PromotionPayChannelResponse> paginationExtendBean = new PaginationExtendBean<>();
        paginationExtendBean.setRows(promotionService.getList(promotionPayChannelQueryRequest));
        paginationExtendBean.setTotal(promotionService.getCount(promotionPayChannelQueryRequest));
        return new ResponseData<>(paginationExtendBean);
    }
    
    @ApiOperation(value = "获得支付营销详情")
    @GetMapping("/payment/promotion/{id}")
    public ResponseData<PromotionPayChannelResponse> getPaymentPromotion(@PathVariable Integer id) {
        return new ResponseData<>(promotionService.get(id));
    }
}
