package cn.pluss.platform.controller.activityEnroll;


import cn.pluss.platform.activityEnroll.ActivityEnrollService;
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.ActivityEnroll;
import cn.pluss.platform.entity.Dict;
import com.alibaba.fastjson.JSONArray;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
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.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * <p>
 * 商户活动报名记录表 前端控制器
 * </p>
 *
 * @author Djh
 * @since 2020-11-02
 */
@Controller
@RequestMapping("/activityEnroll")
public class ActivityEnrollController extends BaseModelController<ActivityEnrollService, ActivityEnroll> {

    @Autowired
    private DictService dictService;

    @RequestMapping(method = {RequestMethod.GET, RequestMethod.POST}, value = "/index")
    public String index(HttpServletRequest request) {
        List<String> actList = new ArrayList<String>(){{this.add("ACTIVITY_TYPE");}};
        List<Dict> actDictList = dictService.getByCode(actList);
        request.setAttribute("actTypeList",JSONArray.toJSONString(actDictList));
        return "activityEnroll/list";
    }


    @Override
    public Result<Page<ActivityEnroll>> page(Page<ActivityEnroll> page, ActivityEnroll condition, OrderItem orderItem) {
        orderItem.setAsc(false).setColumn("createTime");
        return super.page(page, condition, orderItem);
    }

    @PostMapping("saveOrUpdate")
    @ResponseBody
    public Result<String> saveOrUpdate(@RequestBody ActivityEnroll activityEnroll) {
        if (activityEnroll.getId() != null){
            activityEnroll.setAuditTime(new Date());
            this.baseService.updateById(activityEnroll);
        }else {
            activityEnroll.setCreateTime(new Date());
            this.baseService.save(activityEnroll);
        }
        return ResultGenerator.genSuccessResult("保存成功");
    }
}
