package com.happy.action;

import com.baidubce.services.media.model.Job;
import com.google.common.base.Joiner;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.happy.Model.Admin;
import com.happy.Model.AdminManager;
import com.happy.Model.HotelCoupon;
import com.happy.Model.HotelDict;
import com.happy.Until.Func;
import com.happy.Until.GetHttpParam;
import com.happy.Until.ResUtil;
import com.happy.Until.UUIDUtil;
import com.happy.common.controller.BaseController;
import com.happy.dao.HotelCouponDao;
import com.happy.dto.IPage;
import com.happy.service.HotelCoupomService;
import com.happy.service.HotelCoupomStatusService;
import net.sf.json.JSONObject;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.Arrays;
import java.util.List;

public class hotelCoupomAction extends BaseController implements ServletRequestAware {


    public String ids; //优惠卷id
    public String name; //优惠卷名称
    public String type; //类型
    public String grantStartDate;//发放时间
    public String grantEndDate;//发放时间

    @Resource(name = "hotelCoupomService")
    private HotelCoupomService hotelCoupomService;

    @Resource(name = "hotelCoupomStatusService")
    private HotelCoupomStatusService hotelCoupomStatusService;
    /**
     * 描述：新增优惠卷
     * @return
     */
    public String inster(){
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        HotelCoupon hotelCoupon = null;
        try{
            hotelCoupon = gson.fromJson(json.toString(), new TypeToken<HotelCoupon>() {}.getType());
            if(hotelCoupon==null){
                resultJson.put("message", "数据为空");
                resultJson.put("code", 500);
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            //格式化
            hotelCoupon.setRemainderNumber(hotelCoupon.getGrantNumber());
            hotelCoupon.setReversedNumber(0);
            int m = hotelCoupomService.insert(hotelCoupon);
            if (m > 0) {
                resultJson.put("message", "添加成功");
                resultJson.put("code", 200);
            } else {
                resultJson.put("message", "添加失败");
                resultJson.put("code", 502);
            }
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }catch (Exception e){
            e.printStackTrace();
            resultJson.put("message", "未知异常："+ e);
            resultJson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：修改优惠卷
     * @return
     */
    public String update(){
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        HotelCoupon hotelCoupon = null;
        try{
            hotelCoupon = gson.fromJson(json.toString(), new TypeToken<HotelCoupon>() {}.getType());
            if (hotelCoupon.getId()==null || "".equals(hotelCoupon.getId())){
                resultJson.put("message", "请传入优惠卷id");
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            HotelCoupon h1 = hotelCoupomService.getById(hotelCoupon.getId());
            if (h1==null){
                resultJson.put("message", "修改失败，未查询到优惠卷");
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            int m = hotelCoupomService.update(hotelCoupon);
            if (m > 0) {
                resultJson.put("message", "修改成功");
                resultJson.put("code", 200);
            } else {
                resultJson.put("message", "修改失败");
                resultJson.put("code", 502);
            }
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }catch (Exception e){
            e.printStackTrace();
            resultJson.put("message", "未知异常："+ e);
            resultJson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 描述：分页查询
     * @return
     */
    public String queryPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");
        if (name != null){
            s1.append(" and name like '%").append(name).append("%' ");
        }
        if (status != null){
            s1.append(" and status = ").append(status).append(" ");
        }
        if(type != null){
            s1.append(" and type = ").append(type).append(" ");
        }
        if (grantStartDate != null && grantEndDate != null){
            s1.append(" and DATE_FORMAT(grant_start_date,'%Y-%m-%d') >= '").append(grantStartDate).append("' ");
            s1.append(" and DATE_FORMAT(grant_end_date,'%Y-%m-%d') <= '").append(grantEndDate).append("' ");
        }
        IPage<HotelCoupon> hotelCouponIPage = hotelCoupomService.queryPage(s1.toString(),page,rows);
        if(hotelCouponIPage!=null && !"".equals(hotelCouponIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", hotelCouponIPage);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        return null;
    }

    /**
     * 描述：根据Id查询详细
     * @return
     */
    public String getById(){
        JSONObject resultJson = new JSONObject();
        if (id==null || "".equals(id)) {
            resultJson.put("message", "请传入id");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        HotelCoupon hotelCoupon = hotelCoupomService.getById(id);
        if (hotelCoupon != null) {
            resultJson.put("message", "查询成功");
            resultJson.put("code", 200);
            resultJson.put("data", hotelCoupon);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }else{
            resultJson.put("message", "未查询到优惠卷信息");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
    }

    /**
     * 描述：优惠卷批量失效
     */
    public String saveBatchLapse(){
        JSONObject resultJson = new JSONObject();
        Gson gson = new Gson();
        com.alibaba.fastjson.JSONObject json = GetHttpParam.getRequestParameters(request);
        if (json == null) {
            resultJson.put("message", "请传入参数");
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }
        try {
            if (ids == null){
                resultJson.put("message", "请传入优惠卷id");
                ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
                return null;
            }
            List<String> hotelCoupom = Arrays.asList(ids.split(","));
            int m = hotelCoupomService.updateLapse(hotelCoupom);
            int n = hotelCoupomStatusService.updateLapse(hotelCoupom);
            resultJson.put("message", "修改成功");
            resultJson.put("code", 200);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
            return null;
        }catch (Exception e){
            e.printStackTrace();
            resultJson.put("message", "未知异常："+ e);
            resultJson.put("code", 205);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }
        resultJson.put("message", "未知异常");
        resultJson.put("code", 205);
        ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        return null;
    }

    /**
     * 广告——优惠券分页
     */
    public String advertisingCouponPage(){
        JSONObject resultJson = new JSONObject();
        StringBuilder s1 = new StringBuilder("");

        if (name != null){
            s1.append(" and name like '%").append(name).append("%' ");
        }

        IPage<HotelCoupon> hotelCouponIPage = hotelCoupomService.advertisingCouponPage(s1.toString(),page,rows);

        if(hotelCouponIPage!=null && !"".equals(hotelCouponIPage)){
            resultJson.put("message", "查询分页成功");
            resultJson.put("code", 200);
            resultJson.put("data", hotelCouponIPage);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }else{
            resultJson.put("message", "查询分页失败");
            resultJson.put("code", 500);
            ResUtil.writeJson(ServletActionContext.getResponse(), resultJson.toString());
        }

        return null;
    }

    /**
     * 描述:定时任务修改优惠卷状态
     */
    public void setCoupomStatus(){
        int n =  hotelCoupomService.updateExpire();

        DateTimeFormatter dateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
        //判断当前时间是否在有效期内
        LocalDateTime now = LocalDateTime.now();
        String dateTime = now.format(dateTimeFormatter);
        int m =  hotelCoupomStatusService.updateExpir(dateTime);
    }

}
