package com.iformall.controller;

import com.github.pagehelper.PageInfo;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.po.WxCampaign;
import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.vo.WxCouponChannelVo;
import com.iformall.enums.EnumCampaignStatus;
import com.iformall.enums.EnumCouponChannelType;
import com.iformall.service.WxCampaignService;
import com.iformall.service.WxCouponChannelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
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 javax.servlet.http.HttpServletResponse;
import java.util.List;


@RestController
@RequestMapping("/api/wxCampaign")
@Api(description = "促销和banner接口")
public class WxCampaignController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCampaignService wxCampaignService;
    @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 WxCampaign wxCampaign, Integer pageNum, Integer pageSize) {
        if (null == wxCampaign) wxCampaign = new WxCampaign();

        wxCampaign.setStatus(EnumCampaignStatus.STATUS_THROW_IN.getCode());
        wxCampaign.setTenantId(getTenantId());
        wxCampaign.setSortColumns(BaseEntity.SortField.SortNum_ASC, BaseEntity.SortField.CreateTime_ASC);
        final PageInfo<WxCampaign> page = wxCampaignService.clistAsPage(wxCampaign, pageNum, pageSize);
        return new ResultData(page);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findById(Long id) {
        WxCampaign wxCampaign = wxCampaignService.getById(id);
        if (wxCampaign != null) {
            WxCouponChannel wxCouponChannel = new WxCouponChannel();
            wxCouponChannel.setTenantId(getTenantId());
            wxCouponChannel.setTargetAd(EnumCouponChannelType.COUPON_CHANNEL_ID_CAMPAIN.getCode());
            wxCouponChannel.setSubTargetId(wxCampaign.getId());
            wxCouponChannel.setStatus(0);
            List<WxCouponChannelVo> couponList = wxCouponChannelService.listVo(wxCouponChannel);
            wxCampaign.setCoupons(couponList);
        }
        return new ResultData(Result.SUCCESS, "查询成功", wxCampaign);
    }

    @ApiOperation("调整顺序")
    @GetMapping("/move")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "sourceId", value = "", dataType = "Long", paramType = "query", required = true),
            @ApiImplicitParam(name = "targetId", value = "", dataType = "Long", paramType = "query", required = true)})
    public ResultData move(Long sourceId, Long targetId) {
        WxCampaign source = wxCampaignService.getById(sourceId);
        WxCampaign target = wxCampaignService.getById(targetId);
        if (source == null || target == null) {
            return new ResultData(Result.ERROR, "调整顺序失败", null);
        }
        int temp = source.getSortNum();
        source.setSortNum(target.getSortNum());
        target.setSortNum(temp);
        wxCampaignService.saveOrUpdate(source);
        wxCampaignService.saveOrUpdate(target);
        return new ResultData(Result.SUCCESS, "调整顺序成功", null);
    }

    @ApiOperation("输出富文本内容")
    @GetMapping("/printHtmlById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public void printHtmlById(Long id,HttpServletResponse response){
        wxCampaignService.printHtmlById(id,response);
    }

}
