package com.winsun.controller;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
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.RestController;

import com.baomidou.mybatisplus.enums.SqlLike;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.winsun.auth.core.annotion.Permission;
import com.winsun.auth.core.base.controller.BaseController;
import com.winsun.auth.core.common.model.ResponseData;
import com.winsun.auth.core.shiro.ShiroUser;
import com.winsun.bean.HhrActivity;
import com.winsun.mapper.ActivityMapper;

/**
 * @Author: chancy
 * @Date: 2020/2/12 17:26
 */
@RestController
@RequestMapping("/activity")
public class ActivityController extends BaseController {

	private static ActivityMapper activityMapper;

	@Autowired
	public ActivityController(ActivityMapper activityMapper) {
		ActivityController.activityMapper = activityMapper;
	}
	
	@Permission(menuname = "查询活动信息", value = "list", method = RequestMethod.POST)
	public ResponseData<Page<HhrActivity>> list(@RequestParam("title") String title,
			@RequestParam("activityType") String activityType, @RequestParam("subclass") String subclass,
			@RequestParam(name = "status", required = false) String status,
			@RequestParam(name = "createTimeStart", required = false) String createTimeStart,
			@RequestParam(name = "createTimeEnd", required = false) String createTimeEnd,
			@RequestParam(name = "pageNo", required = false) int pageIndex,
			@RequestParam(name = "pageSize", required = false) int pageSize) {
		Wrapper<HhrActivity> wrapper = new EntityWrapper<HhrActivity>();
		wrapper.like(StringUtils.isNotBlank(title), "title", title, SqlLike.DEFAULT);
		wrapper.eq(StringUtils.isNotBlank(activityType), "activity_type", activityType);
		wrapper.eq(StringUtils.isNotBlank(subclass), "subclass", subclass);
		
		Page<HhrActivity> page = new Page<>(pageIndex, pageSize);
		List<HhrActivity> list = activityMapper.selectPage(page, wrapper);
		page.setRecords(list);
		return ResponseData.success(page, "查询完毕！");
	}
	
	@Permission(menuname = "新增活动", value = "addActivity", method = RequestMethod.POST)
	public ResponseData<String> addActivity(@RequestParam("title") String title, @RequestParam("desc") String desc,
			@RequestParam("integral") String integral, @RequestParam("activityType") String activityType, 
			@RequestParam("subclass") String subclass,@RequestParam("imageUrl") String imageUrl,
			@RequestParam("sort") String sort, @RequestParam("range") String range){
		
		ShiroUser user = getShiroUser();

		HhrActivity activity = new HhrActivity();
		activity.setTitle(title);
		activity.setDesc(desc);
		activity.setIntegral(Integer.parseInt(integral));
		activity.setActivityType(activityType);
		activity.setSubclass(subclass);
		activity.setSort(Integer.parseInt(sort));
		activity.setRange(range);
		activity.setImageUrl(imageUrl);
		activity.setCreator(user.getName());
		activity.setCreateTime(new Date());
		activity.setUpdateTime(new Date());
		int num = activityMapper.insert(activity);
		if (num > 0) {
			return ResponseData.success("修改完毕！");
		}

		return ResponseData.error("修改失败");
	}
	
	@Permission(menuname = "修改活动", value = "updateActivity", method = RequestMethod.POST)
	public ResponseData<String> addActivity(@RequestParam("id") String id, 
			@RequestParam("title") String title, @RequestParam("desc") String desc,
			@RequestParam("integral") String integral, @RequestParam("activityType") String activityType, 
			@RequestParam("subclass") String subclass,@RequestParam("imageUrl") String imageUrl,
			@RequestParam("sort") String sort, @RequestParam("range") String range){

		HhrActivity activity = new HhrActivity();
		activity.setId(id);
		activity.setTitle(title);
		activity.setDesc(desc);
		activity.setIntegral(Integer.parseInt(integral));
		activity.setActivityType(activityType);
		activity.setSubclass(subclass);
		activity.setSort(Integer.parseInt(sort));
		activity.setRange(range);
		activity.setImageUrl(imageUrl);
		activity.setUpdateTime(new Date());
		int num = activityMapper.updateById(activity);
		
		if (num > 0) {
			return ResponseData.success("修改完毕！");
		}

		return ResponseData.error("修改失败");
	}
	
	@Permission(menuname = "删除活动", value = "deleteActivity", method = RequestMethod.POST)
	public ResponseData<String> deleteActivity(@RequestParam("id") String id){

		HhrActivity activity = new HhrActivity();
		activity.setId(id);
		activity.setDelFlag(1);
		int num = activityMapper.updateById(activity);
		
		if (num > 0) {
			return ResponseData.success("删除完毕！");
		}

		return ResponseData.error("删除失败");
	}
	
}
