package com.jpsoft.shinestar.modules.mobile.controller;

import com.github.pagehelper.Page;
import com.jpsoft.shinestar.modules.base.entity.*;
import com.jpsoft.shinestar.modules.base.service.*;
import com.jpsoft.shinestar.modules.common.dto.MessageResult;
import com.jpsoft.shinestar.modules.common.dto.Sort;
import com.jpsoft.shinestar.modules.common.utils.PojoUtils;
import com.jpsoft.shinestar.modules.common.utils.StringUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.text.SimpleDateFormat;
import java.util.*;

@Slf4j
@RestController
@RequestMapping("/mobile/activityApi")
@Api(description = "移动端活动接口")
public class ActivityApiController {
    private Logger logger = LoggerFactory.getLogger(getClass());

    @Autowired
    private ActivityInfoService activityInfoService;
    @Autowired
    private PersonInfoService personInfoService;
    @Autowired
    private PersonPopedomService personPopedomService;

    @PostMapping("getMyActivityList")
    @ApiOperation(value = "本人活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> getMyActivityList(@RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
                                                 @RequestParam(value="pageSize",defaultValue="20") int pageSize,
                                                 @RequestAttribute String subject) {

        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            Map<String,Object> searchParams = new HashMap<>();
            searchParams.put("createBy", subject);

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time","desc"));

            Page<ActivityInfo> page = activityInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            messageResult.setData(PojoUtils.pageWrapper(page));
            messageResult.setCode(200);
            messageResult.setMessage("查询成功");
            messageResult.setResult(true);

        } catch (Exception ex) {
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);
        }

        return messageResult;
    }

    @PostMapping("getActivityList")
    @ApiOperation(value = "所有活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> getActivityList(@RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
                                               @RequestParam(value="pageSize",defaultValue="20") int pageSize,
                                               @RequestAttribute String subject) {

        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Map<String,Object> searchParams = new HashMap<>();
            searchParams.put("endTime", sdf.format(new Date()));
            searchParams.put("verify", 1);

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time","desc"));

            Page<ActivityInfo> page = activityInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            messageResult.setData(PojoUtils.pageWrapper(page));
            messageResult.setCode(200);
            messageResult.setMessage("查询成功");
            messageResult.setResult(true);

        } catch (Exception ex) {
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);
        }

        return messageResult;
    }

    @PostMapping("getVerifyActivityList")
    @ApiOperation(value = "审核活动列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "isVerify", value = "审核状态(0-待处理,1-已处理)", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<Map> getVerifyActivityList(int isVerify,
                                                     @RequestParam(value="pageIndex",defaultValue="1") int pageIndex,
                                                     @RequestParam(value="pageSize",defaultValue="20") int pageSize,
                                                     @RequestAttribute String subject) {

        MessageResult<Map> messageResult = new MessageResult<>();

        try {
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
            Map<String,Object> searchParams = new HashMap<>();
            searchParams.put("endTime", sdf.format(new Date()));
            searchParams.put("isVerify", isVerify);

            List<Sort> sortList = new ArrayList<>();
            sortList.add(new Sort("create_time","desc"));

            Page<ActivityInfo> page = activityInfoService.pageSearch(searchParams,pageIndex,pageSize,true,sortList);

            messageResult.setData(PojoUtils.pageWrapper(page));
            messageResult.setCode(200);
            messageResult.setMessage("查询成功");
            messageResult.setResult(true);

        } catch (Exception ex) {
            messageResult.setCode(400);
            messageResult.setMessage(ex.getMessage());
            messageResult.setResult(false);
        }

        return messageResult;
    }

    @ApiOperation(value="发起活动")
    @PostMapping("addActivity")
    public MessageResult<ActivityInfo> addActivity(
            String title, String content,
            @RequestAttribute String subject){
        MessageResult<ActivityInfo> msgResult = new MessageResult<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            ActivityInfo activityInfo = new ActivityInfo();
            activityInfo.setId(UUID.randomUUID().toString());
            activityInfo.setTitle(title);
            activityInfo.setContent(content);
            activityInfo.setDelFlag(false);
            activityInfo.setCreateBy(subject);
            activityInfo.setCreateTime(new Date());

            PersonInfo personInfo = personInfoService.get(Long.valueOf(subject));
            activityInfo.setApplicant(personInfo.getName());
            PersonPopedom personPopedom = personPopedomService.get(personInfo.getPopedom());
            activityInfo.setRoomNumber(personPopedom.getCompanyStructureName());

            int affectCount = activityInfoService.insert(activityInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(activityInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="活动修改")
    @PostMapping("updateActivity")
    public MessageResult<ActivityInfo> updateActivity(
            String id, String title, String content,
            @RequestAttribute String subject){
        MessageResult<ActivityInfo> msgResult = new MessageResult<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            ActivityInfo activityInfo = activityInfoService.get(id);
            activityInfo.setTitle(title);
            activityInfo.setContent(content);
            activityInfo.setUpdateBy(subject);
            activityInfo.setUpdateTime(new Date());

            int affectCount = activityInfoService.update(activityInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(activityInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库更新失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="活动设置")
    @PostMapping("setActivity")
    public MessageResult<ActivityInfo> setActivity(
            String id, String title, String frontCover, String startTime, String endTime,
            @RequestAttribute String subject){
        MessageResult<ActivityInfo> msgResult = new MessageResult<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            ActivityInfo activityInfo = activityInfoService.get(id);
            activityInfo.setTitle(title);
            activityInfo.setFrontCover(frontCover);
            activityInfo.setStartTime(sdf.parse(startTime + " 00:00:00"));
            activityInfo.setEndTime(sdf.parse(endTime + " 23:59:59"));
            activityInfo.setApplicationTime(new Date());
            activityInfo.setVerify(0);
            activityInfo.setUpdateBy(subject);
            activityInfo.setUpdateTime(new Date());

            int affectCount = activityInfoService.update(activityInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(activityInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="审核活动")
    @PostMapping("verifyActivity")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "编号", paramType = "form"),
            @ApiImplicitParam(name = "verify", value = "审核状态(0-待审核,1-已批准,2-未批准)", paramType = "form"),
            @ApiImplicitParam(name = "verifyMsg", value = "审核意见", paramType = "form"),
            @ApiImplicitParam(name = "subject", value = "目标(不传)", paramType = "form")
    })
    public MessageResult<ActivityInfo> verifyActivity(
            String id, int verify, String verifyMsg,
            @RequestAttribute String subject){
        MessageResult<ActivityInfo> msgResult = new MessageResult<>();
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");

        try {
            ActivityInfo activityInfo = activityInfoService.get(id);
            activityInfo.setVerify(verify);
            activityInfo.setVerifyMsg(verifyMsg);
            activityInfo.setVerifyTime(new Date());
            activityInfo.setUpdateBy(subject);
            activityInfo.setUpdateTime(new Date());

            int affectCount = activityInfoService.update(activityInfo);

            if (affectCount > 0) {
                msgResult.setResult(true);
                msgResult.setData(activityInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库添加失败");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }

    @ApiOperation(value="获取信息")
    @PostMapping("detail")
    public MessageResult<ActivityInfo> detail(String id){
        MessageResult<ActivityInfo> msgResult = new MessageResult<>();

        try {
            ActivityInfo activityInfo = activityInfoService.get(id);

            if (activityInfo != null) {
                msgResult.setResult(true);
                msgResult.setData(activityInfo);
            } else {
                msgResult.setResult(false);
                msgResult.setMessage("数据库不存在该记录!");
            }
        }
        catch(Exception ex){
            logger.error(ex.getMessage(),ex);

            msgResult.setResult(false);
            msgResult.setMessage(ex.getMessage());
        }

        return msgResult;
    }
}