package com.pcloud.book.group.service.impl;

import com.pcloud.book.group.biz.BookGroupClassifyBiz;
import com.pcloud.book.group.biz.GroupQrcodeBiz;
import com.pcloud.book.group.dto.BookClassifyDTO;
import com.pcloud.book.group.dto.GroupClassifyQrcodeDTO;
import com.pcloud.book.group.dto.GroupQrcode4ClassifyDTO;
import com.pcloud.book.group.dto.GroupQrcodeBaseDTO;
import com.pcloud.book.group.dto.GroupQrcodeInfo4Advertising;
import com.pcloud.book.group.dto.ProAssocGroupAndUserNumberDTO;
import com.pcloud.book.group.service.BookGroupClassifyService;
import com.pcloud.book.group.vo.ClassifyVO;
import com.pcloud.book.group.vo.GroupQrcode4ClassifyVO;
import com.pcloud.common.dto.ResponseDto;
import com.pcloud.common.utils.ResponseHandleUtil;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;

/**
 * @author lily
 * @date 2019/4/28 20:23
 */
@RestController("bookGroupClassifyService")
@RequestMapping("bookGroupClassifyService")
public class BookGroupClassifyServiceImpl implements BookGroupClassifyService {
    @Autowired
    private BookGroupClassifyBiz bookGroupClassifyBiz;
    @Autowired
    private GroupQrcodeBiz groupQrcodeBiz;

    @Override
    @GetMapping("getClassifyQrcodeInfo")
    public ResponseEntity<ResponseDto<GroupClassifyQrcodeDTO>> getClassifyQrcodeInfo(@RequestParam("weixinGroupId") String weixinGroupId) {
        GroupClassifyQrcodeDTO groupClassifyQrcodeDTO = bookGroupClassifyBiz.getClassifyQrcodeInfo(weixinGroupId);
        return ResponseHandleUtil.toResponse(groupClassifyQrcodeDTO);
    }

    @Override
    @PostMapping("getClassifyByIds")
    public ResponseEntity<ResponseDto<Map<Long, String>>> getClassifyByIds(@RequestBody List<Long> weixinGroupClassifyPrimaryIds) {
        Map<Long, String> groupClassifyMap =  bookGroupClassifyBiz.getClassifyByIds(weixinGroupClassifyPrimaryIds);
        return ResponseHandleUtil.toResponse(groupClassifyMap);
    }

    @Override
    @PostMapping("listGroupQrcodeBaseInfo")
    public ResponseEntity<ResponseDto<Map<String, GroupQrcodeBaseDTO>>> listGroupQrcodeBaseInfo(@RequestBody List<String> weixinGroupIds) {
        Map<String, GroupQrcodeBaseDTO> groupQrcodeMap =  bookGroupClassifyBiz.listGroupQrcodeBaseInfo(weixinGroupIds);
        return ResponseHandleUtil.toResponse(groupQrcodeMap);
    }

    @Override
    @GetMapping("getProAssocGroupAndUserNumber")
    public ResponseEntity<ResponseDto<ProAssocGroupAndUserNumberDTO>> getProAssocGroupAndUserNumber(@RequestParam("productId") Long productId) {
        ProAssocGroupAndUserNumberDTO proAssocGroupAndUserNumberDTO = bookGroupClassifyBiz.getProAssocGroupAndUserNumber(productId);
        return ResponseHandleUtil.toResponse(proAssocGroupAndUserNumberDTO);
    }

    @Override
    @PostMapping("listGroupQrcodeInfo4Advertising")
    public ResponseEntity<ResponseDto<Map<String, GroupQrcodeInfo4Advertising>>> listGroupQrcodeInfo4Advertising(@RequestBody List<String> wxGroupIds) {
        Map<String, GroupQrcodeInfo4Advertising> map = groupQrcodeBiz.listGroupQrcodeInfo4Advertising(wxGroupIds);
        return ResponseHandleUtil.toResponse(map);
    }

    @Override
    @ApiImplicitParams({
            @ApiImplicitParam(name = "query", value = "书籍名称", dataType = "string",required = false, paramType = "query"),
            @ApiImplicitParam(name = "proLabelId", value = "专业标签id", dataType = "long",required = false, paramType = "query"),
            @ApiImplicitParam(name = "depLabelId", value = "深度标签id", dataType = "long",required = false, paramType = "query"),
            @ApiImplicitParam(name = "purLabelId", value = "目的标签id", dataType = "long",required = false, paramType = "query")
    })
    @GetMapping("listQrcodeIdsByLabelQuery")
    public ResponseEntity<ResponseDto<List<Long>>> listQrcodeIdsByLabelQuery(
            @RequestParam(value = "query", required = false) String query,
            @RequestParam(value = "proLabelId", required = false) Long proLabelId,
            @RequestParam(value = "depLabelId", required = false) Long depLabelId,
            @RequestParam(value = "purLabelId", required = false) Long purLabelId) {
        List<Long> list = groupQrcodeBiz.listQrcodeIdsByLabelQuery(query, proLabelId, depLabelId, purLabelId);
        return ResponseHandleUtil.toResponse(list);
    }

    @Override
    @PostMapping("filterDeleteId")
   public  ResponseEntity<ResponseDto<List<Long>>> filterDeleteId(@RequestBody List<String >wxGroupIdList){
        List<Long>qrcodeIds =  groupQrcodeBiz.filterDeleteId(wxGroupIdList);
        return ResponseHandleUtil.toResponse(qrcodeIds);
    }

    @Override
    @GetMapping("getPayPrice")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "qrcodeId", value = "qrcodeId", dataType = "long", required = true, paramType = "query"),
            @ApiImplicitParam(name = "wxUserId", value = "wxUserId", dataType = "string", required = true, paramType = "query")
    })
    public ResponseEntity<ResponseDto<BigDecimal>> getPayPrice(@RequestParam("qrcodeId") Long qrcodeId, @RequestParam("wxUserId") String wxUserId) {
        return ResponseHandleUtil.toResponse(bookGroupClassifyBiz.getPayPrice(qrcodeId, wxUserId));
    }

    @Override
    @PostMapping("getGroupClassifyCount")
    public ResponseEntity<ResponseDto<Map<Long, BookClassifyDTO>>> getGroupClassifyCount(@RequestBody List<Long> bookIds) {
        Map<Long, BookClassifyDTO> groupClassifyCount = bookGroupClassifyBiz.getGroupClassifyCount(bookIds);
        return ResponseHandleUtil.toResponse(groupClassifyCount);
    }

    @Override
    @GetMapping("getGroupQrcodeInfo4Classify")
    public ResponseEntity<ResponseDto<GroupQrcode4ClassifyDTO>> getGroupQrcodeInfo4Classify(@RequestParam("wechatUserId") Long wechatUserId,@RequestParam("classifyId") Long classifyId) {
        GroupQrcode4ClassifyVO groupQrcode4ClassifyVO = bookGroupClassifyBiz.getGroupQrcodeInfo4Heals(classifyId, wechatUserId);
        GroupQrcode4ClassifyDTO groupQrcode4ClassifyDTO=new GroupQrcode4ClassifyDTO();
        if(null!=groupQrcode4ClassifyVO){
            BeanUtils.copyProperties(groupQrcode4ClassifyVO,groupQrcode4ClassifyDTO);
        }
        return ResponseHandleUtil.toResponse(groupQrcode4ClassifyDTO);
    }

    @Override
    @GetMapping("getGroupQrcodeByClassify")
    public ResponseEntity<ResponseDto<GroupQrcode4ClassifyDTO>> getGroupQrcodeByClassify(@RequestParam("wechatUserId") Long wechatUserId,@RequestParam("classifyId") Long classifyId){
        GroupQrcode4ClassifyVO groupQrcode4ClassifyVO = bookGroupClassifyBiz.getGroupQrcodeByClassify(classifyId, wechatUserId);
        GroupQrcode4ClassifyDTO groupQrcode4ClassifyDTO=new GroupQrcode4ClassifyDTO();
        if(null!=groupQrcode4ClassifyVO){
            BeanUtils.copyProperties(groupQrcode4ClassifyVO,groupQrcode4ClassifyDTO);
        }
        return ResponseHandleUtil.toResponse(groupQrcode4ClassifyDTO);
    }

    @Override
    public ResponseEntity<ResponseDto<BigDecimal>> getClassifyPrice(Long classifyId) {
        BigDecimal bigDecimal = new BigDecimal(0);
        ClassifyVO classify = bookGroupClassifyBiz.getClassify(classifyId);
        if (classify!=null && classify.getPrice()!=null){
            bigDecimal = classify.getPrice();
        }
        return ResponseHandleUtil.toResponse(bigDecimal);
    }
}
