package com.quinn.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.quinn.common.EmailType;
import com.quinn.dto.req.SearchPage;
import com.quinn.dto.req.SendCode;
import com.quinn.dto.res.AboutDTO;
import com.quinn.dto.res.ResponseDTO;
import com.quinn.intergration.SendBMail;
import com.quinn.pojo.About;
import com.quinn.pojo.Invite;
import com.quinn.service.AboutService;
import com.quinn.service.InviteService;
import com.quinn.utils.QuinnUtils;
import com.quinn.vo.MyPageParam;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author limqsh
 * @since 2022-05-01
 */
@RestController("/wx")
public class WxController extends BaseModelController {

    @Resource
    AboutService aboutService;
    @Resource
    InviteService inviteService;
    @Resource
    SendBMail sendBMail;

    @PostMapping("about/us")
    public ResponseDTO userIndexBlog(SearchPage searchPage){
        Page<About> pageParam = new Page<>(searchPage.getPage(), searchPage.getPageSize());
        aboutService.page(pageParam,new QueryWrapper<About>().orderByDesc("gmt_create"));
        // 结果
        List<About> sayList = pageParam.getRecords();
        List<AboutDTO> aboutDTOS = new ArrayList<>();
        if (!CollectionUtils.isEmpty(sayList)){
            sayList.forEach(x->{
                AboutDTO aboutDTO = new AboutDTO();
                aboutDTO.setId(x.getId());
                aboutDTO.setTitle(x.getTitle());
                aboutDTO.setContent(x.getContent());
                aboutDTO.setGmtCreate(QuinnUtils.getViewStrFromDate(x.getGmtCreate()));
                aboutDTOS.add(aboutDTO);
            });
        }
        MyPageParam myPageParam = new MyPageParam(searchPage.getPage(),searchPage.getPageSize());
        myPageParam.setTotal((int) pageParam.getTotal());
        return ResponseDTO.ok().setPage(myPageParam).setData(aboutDTOS);
    }

    @PostMapping("send/register/code")
    public ResponseDTO sendCode(SendCode sendCode){
        Invite email = inviteService.getOne(new QueryWrapper<Invite>().eq("apply_email", sendCode.getEmail()));
        if (email != null){
            return ResponseDTO.ok().setData("该邮箱已经获取过注册码");
        }
        String code = inviteService.getCode();
        Invite invite = new Invite();
        invite.setUid(QuinnUtils.getUuid());
        invite.setApplyEmail(sendCode.getEmail());
        invite.setCode(code);
        invite.setGmtCreate(QuinnUtils.getTime());
        inviteService.save(invite);
        boolean sendFlag = sendBMail.sendOneParamMail(EmailType.REGISTER,code, sendCode.getEmail());
        if (!sendFlag){
            return ResponseDTO.ok().setData("发送失败,请确认邮箱是否正确！");
        }
        return ResponseDTO.ok().setData("success");
    }

}

