package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.domain.vo.WxCouponChannelVo;
import com.iformall.enums.EnumCouponChannelActivityStatus;
import com.iformall.enums.EnumCouponChannelStatus;
import com.iformall.enums.EnumCouponChannelType;
import io.swagger.annotations.Api;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.iformall.common.ResultData;

import com.iformall.domain.po.WxCouponChannel;
import com.iformall.service.WxCouponChannelService;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;

import java.util.Date;

@RestController
@RequestMapping("/api/wxCouponChannel")
@Api(description = "发券渠道相关接口")
public class WxCouponChannelController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

	@Autowired
    private WxCouponChannelService wxCouponChannelService;

	@ApiOperation("分页列表接口")
    @GetMapping("list")
        @ApiImplicitParams({
    	  @ApiImplicitParam(name="pageNum",value="页数",dataType="int", paramType = "query",required=true),
    	  @ApiImplicitParam(name="pageSize",value="每页条数",dataType="int", paramType = "query",required=true)})
    public ResultData list(@ModelAttribute WxCouponChannel wxCouponChannel,Integer pageNum, Integer pageSize) {
    	if (null == wxCouponChannel) wxCouponChannel = new WxCouponChannel();
        wxCouponChannel.setTenantId(getTenantId());
        wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode());
        wxCouponChannel.setSortColumns(WxCouponChannel.Field.CreateDate_DESC);


        PageInfo<WxCouponChannelVo> page = wxCouponChannelService.listPageCVo(wxCouponChannel, pageNum, pageSize);

        if (wxCouponChannel.getTargetAd()!= null &&
                wxCouponChannel.getTargetAd().equals(EnumCouponChannelType.COUPON_CHANNEL_ID_TIMED.getCode())) {
            Date now = new Date();
            page.getList().stream().forEach(cc->{
                if (cc.getBeginTime().getTime() > now.getTime()) {
                    cc.setActivityStatus(EnumCouponChannelActivityStatus.PREPARED.getCode());
                } else if (cc.getEndTime().getTime() < now.getTime()) {
                    cc.setActivityStatus(EnumCouponChannelActivityStatus.ENDED.getCode());
                } else {
                    cc.setActivityStatus(EnumCouponChannelActivityStatus.STARTED.getCode());
                }
            });
        }

        return new ResultData(page);
    }

}
