package com.iformall.controller.market;

import com.alibaba.fastjson.JSON;
import com.github.pagehelper.PageInfo;
import com.iformall.annotation.SystemControllerLog;
import com.iformall.common.Result;
import com.iformall.common.ResultData;
import com.iformall.controller.app.WxAppinfoController;
import com.iformall.controller.base.BaseController;
import com.iformall.domain.dto.WxCouponChannelDto;
import com.iformall.domain.po.*;
import com.iformall.domain.po.base.BaseEntity;
import com.iformall.domain.vo.WxCouponChannelVo;
import com.iformall.mapper.WxCouponChannelMapper;
import com.iformall.service.QrCodeService;
import com.iformall.service.WxCouponChannelService;
import com.iformall.service.WxCouponService;
import com.iformall.service.WxFlowService;
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;
import java.util.Map;


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

    @Autowired
    private WxCouponChannelService wxCouponChannelService;
    @Autowired
    private WxAppinfoController wxAppinfoController;
    @Autowired
    private WxCouponChannelMapper wxCouponChannelMapper;
    @Autowired
    private QrCodeService qrCodeService;
    @Autowired
    private WxFlowService wxFlowService;
    @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)})
    @SystemControllerLog(description = "券投放-列表")
    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(getTenantId());
        wxCouponChannel.setSortColumns(BaseEntity.SortField.UpdateDate_DESC);
        final PageInfo<WxCouponChannelVo> page = wxCouponChannelService.listPageCVo(wxCouponChannel, pageNum, pageSize);
        return new ResultData(page);
    }


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

        ResultData resultData = wxCouponChannelService.saveOrUpdate(wxCouponChannel);
        WxCouponChannel couponChannel = (WxCouponChannel)resultData.data;
        //生成二维码
        if(couponChannel!=null){
            String pageUrl = "pages/index/index";
            String param = couponChannel.getWeappScene();
            ResultData resultDataQ = qrCodeService.uploadQrcode(couponChannel.getTenantId(),1,pageUrl,param,0,"","","卷");
            Map<String,String> map = (Map)resultDataQ.data;
            if(map!=null && map.get("url") !=null) {
                String url = map.get("url");
                couponChannel.setQrCode(url);
                wxCouponChannelMapper.updateById(couponChannel);
            }
        }
        return resultData;
    }

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

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

    @ApiOperation("根据id查询接口")
    @GetMapping("/findChannelByCouponId")
    @ApiImplicitParam(name = "id", value = "id", dataType = "Long", paramType = "query", required = true)
    @SystemControllerLog(description = "券投放-查询")
    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));
    }


}
