package com.yunzhi.marketing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.entity.TaUv;
import com.yunzhi.marketing.service.ITaUvService;
import com.yunzhi.marketing.base.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;
import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 * 用户访问表 前端控制器
 * </p>
 *
 * @author jobob
 * @since 2019-05-10
 */
@RestController
@RequestMapping("/api")
public class TaUvController extends BaseController {

    @Autowired
    ITaUvService taUvService;

    @PostMapping("/wx/{typeOf}/hot/{id}")
    public ResponseBean save(@PathVariable String typeOf, @PathVariable String id, HttpServletRequest request) {
        String openid = getOpenId(request);
        return taUvService.saveData(openid, typeOf, id);
    }

    @GetMapping("/wx/{typeOf}/hot/{id}")
    public ResponseBean hotList(@PathVariable String typeOf, @PathVariable String id, Integer pageNumber, Integer pageSize, HttpServletRequest request) {
        if (null == pageNumber) pageNumber = 1;
        if (null == pageSize) pageSize = 10;

        return taUvService.getListOf(id, typeOf, pageNumber, pageSize);
    }


    /**
     * 测试：重复插入数据 ta_uv 表
     */
    //@RequestMapping(value = "/admin/test/uv", method = RequestMethod.GET)
    public  ResponseBean testUv(HttpServletRequest request, @RequestParam("beUv") String beUv, @RequestParam("num") Integer num){
        ResponseBean responseBean = new ResponseBean();
        QueryWrapper<TaUv> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("be_uv", beUv);
        queryWrapper.eq("tagert_type", "project");
        List<TaUv> list = taUvService.list(queryWrapper);

//        TaUv uv = new TaUv();
//        uv.setPersonId("8a79cdad85182211abeab6adcafd5830");
//        uv.setNameOrnick("党党");
//        uv.setPhotoOravatar("https://wx.qlogo.cn/mmopen/vi_32/ywmrMa0pkIVrPwNAedLt58eKMlicQSHce2M0TUBJFdFIH5pYPPMVfbKJHJJNicDfVrfRVuGXpn3xseJCTTT5nqibg/132");
//        uv.setBeUv("2dd1dadd467751adcc106cb7cb1ab76d");
//        uv.setTagertType("project");
//        uv.setCreateDate(LocalDateTime.of(2019, 9, 9, 10, 10));
//        taUvService.save(uv);

        int i = 0;
        for (int t = 0; t < num; t++) {
            List<TaUv> collect = list.stream().map(e -> {
                e.setCreateDate(LocalDateTime.of(2019, 9, 9, 10, 10));
                e.setUvId(null);
                return e;
            }).collect(Collectors.toList());

            taUvService.saveBatch(collect);
        }

        return responseBean;
    }
}
