package com.slodon.b2b2c.controller.member.front.advich;

import com.alibaba.fastjson.JSON;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.i18n.Language;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.member.example.MemberDownloadCenterExample;
import com.slodon.b2b2c.member.example.MemberEnquiryExample;
import com.slodon.b2b2c.member.pojo.Member;
import com.slodon.b2b2c.member.pojo.MemberDownloadCenter;
import com.slodon.b2b2c.member.pojo.MemberEnquiry;
import com.slodon.b2b2c.model.member.advich.MemberDownloadCenterModel;
import com.slodon.b2b2c.model.member.advich.MemberEnquiryModel;
import com.slodon.b2b2c.system.dto.EnquiryAddDTO;
import com.slodon.b2b2c.vo.member.MemberDownloadCenterVO;
import com.slodon.b2b2c.vo.member.MemberEnquiryVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.List;

/**
 * @author cyan
 */
@Api(tags = "front-下载中心管理")
@RestController
@RequestMapping("v3/member/front/download")
@Slf4j
public class MemberDownloadCenterController extends BaseController {

    @Resource
    private MemberDownloadCenterModel memberDownloadCenterModel;

    @Resource
    private StringRedisTemplate stringRedisTemplate;

    /**
     * 存储下载记录
     *
     * @param request
     * @param downloadCenter
     * @return
     */
    @PostMapping("/add")
    @ResponseBody
    public JsonResult<Object> saveDownloadRecord(HttpServletRequest request, MemberDownloadCenter downloadCenter) throws Exception {
        log.info("********** saveDownloadRecord - downloadCenter:{} *************", JSON.toJSONString(downloadCenter));
        if (downloadCenter.getGoodsId() == null || downloadCenter.getAnnexId() == null) {
            return SldResponse.fail("文档下载记录保存失败,请重试");
        }
        Integer key = memberDownloadCenterModel.saveDownloadRecord(request, downloadCenter);
        return SldResponse.success(key);
    }

    /**
     * 下载记录列表
     *
     * @param request
     * @return
     */
    @GetMapping("list")
    public JsonResult<PageVO<MemberDownloadCenterVO>> getList(HttpServletRequest request,String goodsName) {
        Member member = UserUtil.getUser(request, Member.class);

        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        MemberDownloadCenterExample example = new MemberDownloadCenterExample();
        example.setMemberId(member.getMemberId());
        example.setGoodsName(goodsName);
        example.setPager(pager);
        List<MemberDownloadCenter> list = memberDownloadCenterModel.getDownloadCenterListByPage(example, pager);
        pager.setRowsCount(memberDownloadCenterModel.downloadReacordListByPageCount(example));
        ArrayList<MemberDownloadCenterVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(downloadCenter -> {
                vos.add(new MemberDownloadCenterVO(downloadCenter));
            });
        }
        return SldResponse.success(new PageVO<>(vos, example.getPager()));
    }
}
