package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.ResultData;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.po.WxMerchant;
import com.iformall.domain.vo.WxCouponChannelVo;
import com.iformall.enums.EnumCouponChannelActivityStatus;
import com.iformall.enums.EnumCouponChannelStatus;
import com.iformall.enums.EnumCouponChannelType;
import com.iformall.enums.EnumCouponStatus;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxMerchantService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.collections.map.HashedMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Date;
import java.util.Map;

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

	@Autowired
    private WxCouponChannelService wxCouponChannelService;

    @Autowired
    private WxMerchantService wxMerchantService;

	@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.setCouponStatus(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode());

        wxCouponChannel.setSortColumns(BaseEntity.SortField.UpdateDate_DESC, BaseEntity.SortField.Id_DESC);
        PageInfo<WxCouponChannelVo> page = wxCouponChannelService.listPageCVo(wxCouponChannel, pageNum, pageSize);
        filterTimed(wxCouponChannel, page);
        return new ResultData(page);
    }


    @ApiOperation("商户活动列表信息")
    @GetMapping("listByMerchant")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "pageNum", value = "页数", dataType = "int", paramType = "query", required = true),
            @ApiImplicitParam(name = "pageSize", value = "每页条数", dataType = "int", paramType = "query", required = true)})
    public ResultData listByMerchant(@ModelAttribute WxCouponChannel wxCouponChannel, Integer pageNum, Integer pageSize) {
        wxCouponChannel.setTenantId(getTenantId());
        wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode());
        wxCouponChannel.setSortColumns(BaseEntity.SortField.UpdateDate_DESC, BaseEntity.SortField.Id_DESC);

        Map<String,Object> map = new HashedMap();
        PageInfo<WxCouponChannelVo> page = wxCouponChannelService.listByMerchant(wxCouponChannel,pageNum,pageSize);
        filterTimed(wxCouponChannel, page);

        if(wxCouponChannel.getMerchantId() != null){
            WxMerchant wm = wxMerchantService.getById(wxCouponChannel.getMerchantId());
            map.put("qrCode",wm.getQrCode());
        }

        map.put("page",page);
        return new ResultData(map);
    }

    private void filterTimed(WxCouponChannel wxCouponChannel, PageInfo<WxCouponChannelVo> page) {
        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());
                }
            });
        }
    }


    @ApiOperation("分页列表接口")
    @GetMapping("change")
    public ResultData change(@ModelAttribute WxCouponChannel wxCouponChannel) {
        if (null == wxCouponChannel) wxCouponChannel = new WxCouponChannel();
        wxCouponChannel.setTenantId(getTenantId());
        wxCouponChannel.setStatus(EnumCouponChannelStatus.STATUS_THROW_IN.getCode());
        wxCouponChannel.setCouponStatus(EnumCouponStatus.COUPON_STATUS_THROW_IN.getCode());
        wxCouponChannel.setSortColumns(BaseEntity.SortField.UpdateDate_DESC, BaseEntity.SortField.Id_DESC);
        return wxCouponChannelService.change(wxCouponChannel);
    }


}
