package com.ssj.sys.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ssj.bean.common.framework.core.domain.Response;
import com.ssj.bean.sys.codeexchange.CodeExchange;
import com.ssj.framework.basic.common.bean.SplitPage;
import com.ssj.framework.basic.utils.DateHelper;
import com.ssj.framework.core.common.controller.BaseController;
import com.ssj.framework.core.util.StringUtil;
import com.ssj.service.sys.codeexchange.CodeExchangeService;
import com.ssj.sys.util.j2se.RandomUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author Mount Liu
 * @date 2018/8/22
 */
@Controller
@RequestMapping("/sys/codeexchange")
public class CodeExchangeController extends BaseController {

    @Autowired
    private CodeExchangeService codeExchangeService;

    @RequestMapping("/codeList")
    public String codeList(Model model,
                              @RequestParam(required = false, defaultValue = "10") int pageSize,
                              @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {
        Map<String, Object> params = new HashMap<>(16);
        params.put("code", request.getParameter("LIKE_code") == null ? "" : request.getParameter("LIKE_code"));
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<CodeExchange> page = codeExchangeService.findByPage(pageNo, pageSize, params);
        sp.setRowCnt((int) page.getTotal());
        model.addAttribute("list", page.getRecords());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/codeexchange/codeList";
    }

    @RequestMapping("/codeShopList")
    public String codeShopList(Model model,
                           @RequestParam(required = false, defaultValue = "10") int pageSize,
                           @RequestParam(required = false, defaultValue = "1") int pageNo) throws Exception {
        Map<String, Object> params = new HashMap<>(16);
        params.put("code", request.getParameter("LIKE_code") == null ? "" : request.getParameter("LIKE_code"));
        params.put("module", 2);
        SplitPage sp = new SplitPage();
        sp.setAction(request.getRequestURI());
        sp.setPageNo(pageNo);
        sp.setPageSize(pageSize);
        sp.setParams(params);
        Page<CodeExchange> page = codeExchangeService.findByPage(pageNo, pageSize, params);
        sp.setRowCnt((int) page.getTotal());
        model.addAttribute("list", page.getRecords());
        model.addAttribute("listNavigatHtml", sp.getSysPaginHtml());
        model.addAttribute("page", sp);
        model.addAttribute("search", params);
        return "sys/codeexchange/codeShopList";
    }

    @RequestMapping("/codeDetail")
    public String codeDetail(Model model, @RequestParam(defaultValue = "0") String id) {
        CodeExchange entity = codeExchangeService.getById(id);
        if (entity == null) {
            entity = new CodeExchange();
        }
        model.addAttribute("entity", entity);
        return "sys/codeexchange/codeDetail";
    }

    @RequestMapping(value = "/codeGenerate", method = RequestMethod.POST)

    @ResponseBody
    public Response codeGenerate(CodeExchange entity) {
        logger.info(entity.toString());
        Response response = new Response();
        if (StringUtil.isBlank(entity.getId())) {
            entity.setCreateTime(new Date());
        }
        new Thread(() -> {
            for (int i = 0; i < entity.getNumber(); i++) {
                String code = RandomUtils.getRandomSpecialString(16);
                //生成优惠码
                CodeExchange codeExchange = new CodeExchange();
                codeExchange.setCode(code);
                codeExchange.setCreateTime(new Date());
                codeExchange.setIsValid(1);
                //暂时写死：作业吧服务
                codeExchange.setModule(1);
                codeExchange.setType(1);
                codeExchange.setDays(entity.getDays());
                codeExchange.setMonths(entity.getMonths());
                codeExchange.setStartTime(entity.getStartTime());
                codeExchange.setEndTime(entity.getEndTime());
                codeExchangeService.save(codeExchange);
            }
        }).start();
        response.success();
        return response;
    }

    @RequestMapping(value = "/shopcodeGenerate", method = RequestMethod.POST)

    @ResponseBody
    public Response shopcodeGenerate(CodeExchange entity) {
        Response response = new Response();
        logger.info(entity.toString());
        Date now = new Date();
        for (int i = 0; i < entity.getNumber(); i++) {
            //生成优惠码
            CodeExchange codeExchange = new CodeExchange();
            //生成优惠码
            codeExchange.setCode(RandomUtils.getRandomSpecialString(16));
            codeExchange.setCreateTime(now);
            codeExchange.setIsValid(1);
            //2：电商优惠券兑换
            codeExchange.setModule(2);
            codeExchange.setStartTime(now);
            codeExchange.setEndTime(DateHelper.parseDate("9999-01-01", "yyyy-MM-dd"));
            codeExchangeService.save(codeExchange);
        }
        response.success();
        return response;
    }

    @RequestMapping(value = "/codeDelete", method = RequestMethod.POST)
    @ResponseBody
    public Response codeDelete(String id) {
        Response response = new Response();
        codeExchangeService.delete(id);
        response.success();
        return response;
    }

    @RequestMapping("/codeShopEdit")
    public String merchantAdd(Model model, String id) throws Exception {
        CodeExchange codeExchange = codeExchangeService.getById(id);
        model.addAttribute("entity", codeExchange);
        return "sys/codeexchange/codeShopEdit";
    }

    @RequestMapping("/codeSave")
    @ResponseBody
    public Response codeSave(Model model, CodeExchange entity) throws Exception {
        Response response = new Response();
        CodeExchange codeExchange = codeExchangeService.getById(entity.getId());
        codeExchange.setIsValid(entity.getIsValid());
        codeExchangeService.save(codeExchange);
        response.success("成功");
        return response;
    }
}
