package com.iformall.controller;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.iformall.common.ErrorCode;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.domain.dto.WxCouponChannelDto;
import com.iformall.domain.po.MallUserInfo;
import com.iformall.domain.po.WxCoupon;
import com.iformall.domain.po.WxCouponChannel;
import com.iformall.domain.vo.WxCouponChannelVo;
import com.iformall.enums.EnumCouponChannelStatus;
import com.iformall.enums.EnumCouponStatus;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxCouponService;
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.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("wxCouponChannel")
@Api(description = "优惠券投放接口")
public class WxCouponChannelController extends BaseController {
    private final Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private WxCouponChannelService wxCouponChannelService;

    @Autowired
    private WxCouponService wxCouponService;


    @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) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::list");
        if (null == wxCouponChannel) wxCouponChannel = new WxCouponChannel();
        if (wxCouponChannel.getStatus() != null && wxCouponChannel.getStatus() == -1) {
            wxCouponChannel.setStatus(null);
        }
        wxCouponChannel.setTenantId(getUser().getTenantId());
        wxCouponChannel.setSortColumns(WxCouponChannel.Field.Id_DESC);
        final PageInfo<WxCouponChannelVo> page = wxCouponChannelService.listPageCVo(wxCouponChannel, pageNum, pageSize);
        return new ResultData(page);
    }


    @ApiOperation("根据id更新接口")
    @PostMapping("update")
    public ResultData update(@RequestBody WxCouponChannel wxCouponChannel) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::update");
        wxCouponChannel.setTenantId(getUser().getTenantId());

        return wxCouponChannelService.saveOrUpdate(wxCouponChannel);
    }

    @ApiOperation("根据id删除接口")
    @GetMapping("/del")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData delete(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::delete");
        wxCouponChannelService.deleteById(id);
        return new ResultData(Result.SUCCESS, "删除成功", null);
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findById")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findById(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::findById");
        return new ResultData(Result.SUCCESS, "查询成功", wxCouponChannelService.getById(id));
    }

    @ApiOperation("批量新增")
    @PostMapping("/addbatch")
    public ResultData addbatch(@RequestBody WxCouponChannelDto wxCouponChannelDto) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::addbatch");
        String[] ids = wxCouponChannelDto.getCouponIds().split(",");
        String[] channelId = wxCouponChannelDto.getChannelId().split(",");
        MallUserInfo user = getUser();
        return wxCouponChannelService.addBatch(ids, channelId, user.getTenantId(), wxCouponChannelDto.getBeginTime(), wxCouponChannelDto.getEndTime());
    }

    @ApiOperation("根据id查询接口")
    @GetMapping("/findChannelByCouponId")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    public ResultData findChannelByCouponId(Long id) {
        logger.debug("[" + getIpAddr() + "] WxCouponChannelController::findChannelByCouponId");
        List<Integer> channellist = new ArrayList<>();
        WxCouponChannel wxCouponChannel = new WxCouponChannel();
        wxCouponChannel.setTenantId(getTenantId());
        wxCouponChannel.setStatus(0);
        wxCouponChannel.setCouponId(id);
        List<WxCouponChannel> list = wxCouponChannelService.listAsPage(wxCouponChannel, 1, 5).getList();
        if (list.isEmpty()) {
            return new ResultData(Result.SUCCESS, "查询成功", "");
        }
        for (WxCouponChannel temp : list) {
            channellist.add(temp.getTargetAd());
        }
        return new ResultData(Result.SUCCESS, "查询成功", JSON.toJSONString(channellist));
    }


}
