package com.dgtis.dorm.manager.admin.web;

import com.github.pagehelper.PageInfo;
import com.dgtis.dorm.manager.admin.annotation.LoginAdmin;
import com.dgtis.dorm.manager.db.domain.*;
import com.dgtis.dorm.manager.db.service.AdminService;
import com.dgtis.dorm.manager.db.service.NewsOrganizeService;
import com.dgtis.dorm.manager.db.service.NewsService;
import com.dgtis.dorm.manager.db.service.UserService;
import com.dgtis.dorm.manager.db.util.ResponseUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.*;

/**
 * @descption: 内容与互动管理
 * @auther: qxm
 * @date: 2019/4/22 17:03
 */
@RestController
@RequestMapping("/admin/news")
public class NewsController {

    @Autowired
    private NewsService newsService;
    @Autowired
    private AdminService adminService;
    @Autowired
    private NewsOrganizeService newsOrganizeService;
    @Autowired
    private UserService userService;

    /**
     * @descption: 获取 交互内容列表
     * @param: [adminId, newsCriterion]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:04
     */
    @PostMapping("list")
    public Object list(@LoginAdmin Integer adminId, @RequestBody NewsCriterion newsCriterion) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        News news = new News();
        news.setNewsCriterion(newsCriterion);

        List<News> lstNews = newsService.querySelective(news);
        int total = newsService.countSelective(news);

        if(StringUtils.isBlank(newsCriterion.getType())) {
            List<News> lstRemove = new ArrayList<News>();
            for(News newsTemp : lstNews) {

                if(newsTemp.getType().equals("administrationInfo")) {
                    newsTemp.setTitle("[行政动态]-" + newsTemp.getTitle());
                } else if(newsTemp.getType().equals("servicePacesetter")) {
                    newsTemp.setTitle("[服务标兵]-" + newsTemp.getTitle());
                } else if(newsTemp.getType().equals("centraActivities")) {
                    newsTemp.setTitle("[中心活动]-" + newsTemp.getTitle());
                } else if(newsTemp.getType().equals("reading")) {
                    newsTemp.setTitle("[悦读]-" + newsTemp.getTitle());
                } else if(newsTemp.getType().equals("notice")) {
                    newsTemp.setTitle("[公告管理]-" + newsTemp.getTitle());
                }
            }

            if(lstRemove.size() > 0) {
                lstNews.removeAll(lstRemove);
                total = total - lstRemove.size();
            }
        } /*else if(newsCriterion.getType().equals("activity")) {
            LocalDateTime localDate = LocalDateTime.now();

            for(News newsItem : lstNews) {
                // 当前时间小于报名开始时间：报名未开始
                if(newsItem.getRegistStartDate() != null && localDate.compareTo(newsItem.getRegistStartDate()) < 0) {
                    newsItem.setActivityState("报名待开始");
                } else if(newsItem.getRegistEndDate() != null && localDate.compareTo(newsItem.getRegistEndDate()) < 0) {
                    // 当前时间小于报名截止时间：报名进行中
                    newsItem.setActivityState("报名进行中");
                } else if(newsItem.getRegistEndDate() != null && localDate.compareTo(newsItem.getRegistEndDate()) > 0
                        && newsItem.getActivityStartDate() != null && localDate.compareTo(newsItem.getActivityStartDate()) < 0) {
                    // 当前时间大于报名截止时间且小于活动开始时间：报名已截止
                    newsItem.setActivityState("报名已截止");
                } else if(newsItem.getActivityStartDate() != null && localDate.compareTo(newsItem.getActivityStartDate()) > 0
                        && newsItem.getActivityEndDate() != null && localDate.compareTo(newsItem.getActivityEndDate()) < 0) {
                    // 当前时间在活动开始时间和活动结束时间之间：活动进行中
                    newsItem.setActivityState("活动进行中");
                } else if(newsItem.getActivityEndDate() != null && localDate.compareTo(newsItem.getActivityEndDate()) > 0) {
                    // 当前时间大于活动结束时间：活动已结束
                    newsItem.setActivityState("活动已结束");
                }
            }
        }*/

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("total", total);
        data.put("items", lstNews);

        return ResponseUtil.ok(data);
    }

    /**
     * @descption: 获取交互内容 详情
     * @param: [adminId, id]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:38
     */
    @GetMapping("read")
    public Object read(@LoginAdmin Integer adminId, Integer id) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        if(id == null){
            return ResponseUtil.badArgument();
        }

        News news = newsService.findById(id);

        return ResponseUtil.ok(news);
    }

    /**
     * @descption: 创建交互内容
     * @param: [adminId, news]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:57
     */
    @PostMapping("create")
    public Object create(@LoginAdmin Integer adminId, @RequestBody News news) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Admin admin = adminService.findById(adminId);
        if(admin != null) {
            news.setCreator(admin.getNickname());
            news.setIsAdmin("1");//标识为管理员创建(用于另外提取以关联名字头像)

            // 根据手机号查询用户关联的微信用户id
            if(StringUtils.isNotBlank(admin.getPhone())) {
                AlumniUser alumniUser = new AlumniUser();
                alumniUser.setPhone(admin.getPhone());

                PageInfo<AlumniUser> pageInfoUser = userService.querySelective(alumniUser, 1, 10, null, null);
                List<AlumniUser> lstUser = pageInfoUser.getList();
                if(lstUser != null && lstUser.size() == 1) {
                    news.setCreatorId(lstUser.get(0).getId().toString());
                }
            }
        }

        //news.setCreatorId(String.valueOf(adminId));
        news.setCreateDate(LocalDateTime.now());
        newsService.add(news);

        if(news.getOrganizeId() != null) {
            for(int organizeId : news.getOrganizeId()) {
                NewsOrganize newsOrganize = new NewsOrganize();
                newsOrganize.setNewsId(news.getId());
                newsOrganize.setOrganizeId(organizeId);
                newsOrganize.setUserId(adminId);
                newsOrganize.setFromType("0");

                newsOrganizeService.add(newsOrganize);
            }
        }

        return ResponseUtil.ok(news);
    }

    /**
     * @descption: 更新交互内容
     * @param: [adminId, news]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:57
     */
    @PostMapping("update")
    public Object update(@LoginAdmin Integer adminId, @RequestBody News news) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        newsService.updateById(news);

        newsOrganizeService.deleteByActivityId(news.getId());
        if(news.getOrganizeId() != null) {
            for(int organizeId : news.getOrganizeId()) {
                NewsOrganize newsOrganize = new NewsOrganize();
                newsOrganize.setNewsId(news.getId());
                newsOrganize.setOrganizeId(organizeId);
                newsOrganize.setFromType("1");

                newsOrganizeService.add(newsOrganize);
            }
        }

        return ResponseUtil.ok(news);
    }
    
    
    /**
     * @descption: 更新审核状态
     * @param: [adminId, news]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:56
     */
    @PostMapping("updateNewsAuth")
    public Object updateNewsAuth(@LoginAdmin Integer adminId, @RequestBody News news) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        newsService.updateById(news);

        return ResponseUtil.ok(news);
    }
    
    
    /**
     * @descption: 删除交互内容
     * @param: [adminId, news]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:56
     */
    @PostMapping("delete")
    public Object delete(@LoginAdmin Integer adminId, @RequestBody News news) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        Integer id = news.getId();
        if(id == null) {
            return ResponseUtil.badArgument();
        }

        newsService.deleteById(id);

        return ResponseUtil.ok();
    }
    /**
     * @descption: 批量删除交互内容
     * @param: [adminId, newsId]
     * @return: java.lang.Object
     * @auther: qxm
     * @date: 2019/4/22 17:55
     */
    @PostMapping("deleteQuantity")
    public Object deleteQuantity(@LoginAdmin Integer adminId, String newsId) {
        if (adminId == null) {
            return ResponseUtil.unlogin();
        }
        if(newsId == null) {
            return ResponseUtil.badArgument();
        }
        newsService.deleteQuantity(Arrays.asList(newsId.split(",")));
        return ResponseUtil.ok();
    }
    /**
     * 获取 活动关联的圈子信息
     * @param adminId
     * @param newsId
     * @return
     */
    @GetMapping("listActivityOrg")
    public Object listActivityOrg(@LoginAdmin Integer adminId, int newsId) {

        if(adminId == null) {
            return ResponseUtil.unlogin();
        }

        NewsOrganize newsOrganize = new NewsOrganize();
        newsOrganize.setNewsId(newsId);
        List<NewsOrganize> lstNewsOrganize = newsOrganizeService.findNewsOrganize(newsOrganize);

        Map<String, Object> data = new HashMap<String, Object>();
        data.put("items", lstNewsOrganize);

        return ResponseUtil.ok(data);
    }
}
