package com.willalp.web.controller.news;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.willalp.app.domain.HsAppHomeData;
import com.willalp.app.service.IHsAppHomeDataService;
import com.willalp.common.annotation.Log;
import com.willalp.common.core.controller.BaseController;
import com.willalp.common.core.domain.AjaxResult;
import com.willalp.common.core.redis.RedisCache;
import com.willalp.common.enums.BusinessType;
import com.willalp.common.exception.base.BaseException;
import com.willalp.common.utils.DateUtils;
import com.willalp.common.utils.SecurityUtils;
import com.willalp.common.utils.poi.ExcelUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

/**
 * app首页数据Controller
 *
 * @author willalp
 * @date 2022-01-18
 */
@RestController
@RequestMapping("/news/ad")
public class HsAppHomeDataController extends BaseController {
    @Autowired
    private IHsAppHomeDataService hsAppHomeDataService;

    /**
     * 查询app首页数据列表
     */
    @GetMapping("/list")
    public AjaxResult list(HsAppHomeData hsAppHomeData) {
        List<HsAppHomeData> list = hsAppHomeDataService.selectHsAppHomeDataList(hsAppHomeData);
        return AjaxResult.success(list);
    }

    /**
     * 导出app首页数据列表
     */
    @Log(title = "app首页数据", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(HsAppHomeData hsAppHomeData) {
        List<HsAppHomeData> list = hsAppHomeDataService.selectHsAppHomeDataList(hsAppHomeData);
        ExcelUtil<HsAppHomeData> util = new ExcelUtil<HsAppHomeData>(HsAppHomeData.class);
        return util.exportExcel(list, "app首页数据数据");
    }

    /**
     * 获取app首页数据详细信息
     */
    @GetMapping(value = "/{homeContentId}")
    public AjaxResult getInfo(@PathVariable("homeContentId") Long homeContentId) {
        return AjaxResult.success(hsAppHomeDataService.getById(homeContentId));
    }

    /**
     * 新增app首页数据
     */
    @Log(title = "app首页数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody HsAppHomeData hsAppHomeData) {
        try {
            hsAppHomeDataService.saveHsAppHomeData(hsAppHomeData);
        } catch (BaseException be) {
            return AjaxResult.error(be.getMessage());
        }
        return AjaxResult.success();
    }

    /**
     * 修改app首页数据
     */
    @Log(title = "app首页数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody HsAppHomeData hsAppHomeData) {
        hsAppHomeData.setUpdateBy(SecurityUtils.getUsername());
        hsAppHomeData.setUpdateTime(DateUtils.getNowDate());
        return toAjax(hsAppHomeDataService.updateById(hsAppHomeData));
    }

    @Resource
    private RedisCache redisCache;
    private final String orgCode = "G00000007";

    /**
     * 删除app首页数据
     */
    @Log(title = "app首页数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/{homeContentIds}")
    public AjaxResult remove(@PathVariable String[] homeContentIds) {
        try {
            if (homeContentIds.length == 0) {
                return AjaxResult.error("请提交ID_ARRAY");
            }
            List<String> ids = new ArrayList<>();
            JSONObject jsonObject = redisCache.getCacheObject(orgCode + "_HomeData");
            List<HsAppHomeData> cacheList = (List<HsAppHomeData>) jsonObject.get("data");
            for (int i = 0; i < homeContentIds.length; i++) {
                ids.add(homeContentIds[i]);
                for (int i1 = 0; i1 < cacheList.size(); i1++) {
                    if (cacheList.get(i1).getHomeParentId().equals(homeContentIds[i])) {
                        cacheList.remove(i1);
                    }
                }
                HsAppHomeData temporaryColumn = hsAppHomeDataService.getBaseMapper().selectOne(new QueryWrapper<HsAppHomeData>()
                        .eq("home_parent_id", homeContentIds[i]));
                if (null != temporaryColumn) {
                    ids.add(temporaryColumn.getHomeContentId());
                }
            }
            if (hsAppHomeDataService.removeByIds(ids)) {
                JSONObject jsonObject1 = new JSONObject();
                cacheList.sort(Comparator.comparing(HsAppHomeData::getUpdateTime).reversed());
                jsonObject1.put("data", cacheList);
                redisCache.setCacheObject(orgCode + "_HomeData", jsonObject1);
                return AjaxResult.success();
            }
            return AjaxResult.error();
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.error();
        }
    }
}
