package com.uas.platform.b2b.manage.controller;

import com.uas.platform.b2b.manage.core.support.SystemSession;
import com.uas.platform.b2b.manage.model.Invite;
import com.uas.platform.b2b.manage.model.UseType;
import com.uas.platform.b2b.manage.service.InviteService;
import com.uas.platform.b2b.manage.service.UseLogService;
import com.uas.platform.b2b.manage.support.DateFormatUtils;
import com.uas.platform.b2b.manage.support.JxlsExcelView;
import com.uas.platform.b2b.manage.support.MyException;
import com.uas.platform.b2b.manage.web.BaseController;
import com.uas.platform.core.model.PageInfo;
import com.uas.platform.core.model.PageParams;
import com.uas.platform.core.util.AgentUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.Date;

/**
 * 邀请注册记录
 *
 * @author hejq
 * @date 2018-07-10 16:17
 */
@RestController
@RequestMapping(value = "/invite")
public class InviteController extends BaseController {

    @Autowired
    private InviteService inviteService;

    /**
     * 通过分页信息获取邀请注册记录
     *
     * @param params 分页参数
     * @param keyword 关键词
     * @param request request
     * @return
     */
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public Page<Invite> findByPageInfo(PageParams params, String keyword, HttpServletRequest request, String fromDate, String endDate) throws ParseException {
        PageInfo info = new PageInfo(params);
        Sort sort = new Sort(Sort.Direction.DESC, "enDate");
        info.setSort(sort);
        if (!StringUtils.isEmpty(keyword)) {
            keyword = keyword.trim();
        }
        useLogService.appendLog(UseType.UAS_INVITE_SEARCH.code(), keyword, AgentUtils.getIp(request));
        return inviteService.findInviteByPageInfo(info, keyword, fromDate, endDate);
    }

    /**
     * 导出邀请注册记录信息
     *
     * @param keyword 关键字
     * @param request request
     * @return ModelAndView
     */
    @RequestMapping(value = "/xls", method = RequestMethod.GET )
    public ModelAndView exportInvites(String keyword, HttpServletRequest request, String fromDate, String endDate) throws ParseException {
        PageParams params = new PageParams();
        params.setPage(1);
        params.setCount(JxlsExcelView.MAX_SIZE);
        ModelAndView modelAndView = new ModelAndView();
        Page<Invite> invites = findByPageInfo(params, keyword, request, fromDate, endDate);
        modelAndView.addObject("time", DateFormatUtils.EXPORT_FORMAT.format(new Date(System.currentTimeMillis())));
        modelAndView.addObject("user", SystemSession.getUser().getFullName());
        modelAndView.addObject("count",
                invites.getTotalElements() > JxlsExcelView.MAX_SIZE ? JxlsExcelView.MAX_SIZE : invites.getTotalElements());
        modelAndView.addObject("data", invites.getContent());
        modelAndView.addObject("dateFormat", DateFormatUtils.EXPORT_FORMAT);
        modelAndView.setView(new JxlsExcelView("classpath:jxls-tpl/Invite", "邀请注册记录"));
        useLogService.appendLog(UseType.EXPROT_INVITE_DATA.code(), keyword, AgentUtils.getIp(request));
        return modelAndView;
    }

    /**
     * 通过企业UU，填写的邀请用户信息，企业信息更新邀请记录
     *
     * @param enUU 被邀请企业UU
     * @param userName 邀请用户姓名
     * @param userTel 邀请用户电话
     * @param userUU 邀请用户UU
     * @param enterName 邀请企业名
     * @param enterUU 邀请企业UU
     * @return ModelMap
     * @throws MyException
     */
    @RequestMapping(value = "/edit", method = RequestMethod.POST)
    public ModelMap editInvite(Long enUU, String userName, String userTel, Long userUU, String enterName, Long enterUU) throws MyException {
        String inviteInfo = "UU： " + enUU + ";"
                + "邀请企业： " + enterName + "(" + enterUU + ");"
                + "邀请人: " + userName + "(tel: " + userTel + ", uu" + userUU + ")";
        useLogService.appendLog(UseType.EDIT_INVITE.code(), inviteInfo, AgentUtils.getIp(request));
        return inviteService.editInvite(enUU, userName, userTel, userUU, enterName, enterUU);
    }

    /**
     * 清除邀请记录
     *
     * @param enUU 注册的企业UU
     * @return
     */
    @RequestMapping(value = "/delete/{enUU}", method = RequestMethod.POST)
    public ModelMap editInvite(@PathVariable("enUU") Long enUU) throws MyException {
        useLogService.appendLog(UseType.DELETE_INVITE.code(), "UU:" + enUU, AgentUtils.getIp(request));
        inviteService.deleteByUU(enUU);
        return success("清除成功");
    }
}
