package cn.pluss.platform.controller.inviteRate;

import cn.pluss.platform.api.Result;
import cn.pluss.platform.api.ResultGenerator;
import cn.pluss.platform.base.BaseModelController;
import cn.pluss.platform.dict.DictService;
import cn.pluss.platform.entity.Dict;
import cn.pluss.platform.entity.InviteRate;
import cn.pluss.platform.entity.OfficialActivity;
import cn.pluss.platform.inviteRate.InviteRateService;
import cn.pluss.platform.officialActivity.OfficialActivityService;
import cn.pluss.platform.util.DateUtils;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

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

/**
 * <p>
 * 邀请商户费率阶梯表 前端控制器
 * </p>
 *
 * @author bzg
 * @since 2020-11-20
 */
@Controller
@RequestMapping("/inviteRate")
public class InviteRateController extends BaseModelController<InviteRateService, InviteRate> {

    @Autowired
    private InviteRateService inviteRateService;

    @Autowired
    private DictService dictService;

    @Autowired
    private OfficialActivityService officialActivityService;

    /**
     * 引导页
     *
     * @return
     */
    @GetMapping("/index")
    public String index(HttpServletRequest request) {
        List<Dict> dictList = dictService.getByCode(Collections.singletonList("SERVICE_PROVIDER_TYPE"));
        request.setAttribute("dictList", JSONObject.toJSONString(dictList));
        return "inviteRate/inviteRateList";
    }

    @Override
    public Result<Page<InviteRate>> page(Page<InviteRate> page, InviteRate condition, OrderItem orderItem) {
        return super.page(page, condition, orderItem);
    }

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = {"/detail/{id}", "/detail"})
    public String detail(HttpServletRequest request, @PathVariable(value = "id", required = false) Integer id) {
        List<Dict> typeList = dictService.getByCode(Collections.singletonList("SERVICE_PROVIDER_TYPE"));
        if (id != null) {
            InviteRate inviteRate = inviteRateService.getById(id);
            String type = inviteRate.getType();
            QueryWrapper<OfficialActivity> queryWrapper = new QueryWrapper<OfficialActivity>().eq("activityGroupNo",type);
            List<OfficialActivity> list = officialActivityService.list(queryWrapper);
            /*String format = "yyyy-MM-dd HH:mm:ss";
            inviteRate.setStartTimeStr(DateFormatUtils.format(inviteRate.getStartTime(), format));
            inviteRate.setEndTimeStr(DateFormatUtils.format(inviteRate.getEndTime(), format));*/
            request.setAttribute("entity", inviteRate);
            request.setAttribute("actList", list);
        }
        request.setAttribute("typeList", typeList);
        return "inviteRate/detail";
    }


    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/getActType/{type}")
    @ResponseBody
    public Result getActType(@PathVariable(value = "type") String type){
        QueryWrapper<OfficialActivity> queryWrapper = new QueryWrapper<OfficialActivity>()
                .eq("status","2").eq("activityGroupNo",type);
        List<OfficialActivity> list = officialActivityService.list(queryWrapper);
        return ResultGenerator.genSuccessResult("查询成功",list);
    }

    @Override
    public Result<String> save(@RequestBody InviteRate entity) {
        OfficialActivity act = officialActivityService.getById(entity.getActId());
        if(act == null){
            return ResultGenerator.genFailResult("活动不存在");
        }
        if (entity.getId() == null){
            entity.setActName(act.getOfficName());
            entity.setStartTime(act.getStartTime());
            entity.setEndTime(act.getEndTime());
            entity.setCreateTime(DateUtils.formatDateDefault(new Date(),"yyyy-MM-dd HH:mm:ss"));
        }else{
            entity.setUpdateTime(DateUtils.formatDateDefault(new Date(),"yyyy-MM-dd HH:mm:ss"));
        }
        return super.save(entity);
    }

    @DeleteMapping("/del")
    public Result<String> delete(@RequestBody InviteRate inviteRate) {
        inviteRateService.removeById(inviteRate.getId());
        return ResultGenerator.genSuccessResult("删除成功", null);
    }
}