package com.heketai.website.web;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.heketai.website.common.result.BaseResult;
import com.heketai.website.entity.WebsiteContentImage;
import com.heketai.website.entity.WebsiteMenuContent;
import com.heketai.website.service.IWebsiteContentImageService;
import com.heketai.website.service.IWebsiteMenuContentService;
import com.heketai.website.common.utils.StringUtils;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@RequestMapping("/menuContent")
@Slf4j
@Api(value = "首页的接口管理",tags = "首页的接口管理")
public class WebsiteMenuContentController {

    @Autowired
    private IWebsiteMenuContentService websiteMenuContentService;

    @Autowired
    private IWebsiteContentImageService websiteContentImageService;

    @Autowired
    private RedisTemplate redisTemplate;

    private String fKey = "F7AB6D60-6AF6-E8D7-0689-51396DC5D2C2";

    /**
     * 新增/修改菜单内容
     *
     * @param websiteMenuContent
     * @return
     */
    @RequestMapping("/updateById")
    public BaseResult updateById(@RequestBody WebsiteMenuContent websiteMenuContent) {
        if (StringUtils.isNull(websiteMenuContent.getId())) {
            int contentId = websiteMenuContentService.insertMenuContent(websiteMenuContent);
            List<WebsiteContentImage> list = websiteMenuContent.getList();
            for (WebsiteContentImage wci : list) {
                wci.setContentId(contentId);
            }
            websiteContentImageService.insertOrUpdateAllColumnBatch(list);
        } else {
            websiteMenuContentService.updateById(websiteMenuContent);
            List<WebsiteContentImage> list = websiteMenuContent.getList();
            for (WebsiteContentImage wci : list) {
                wci.setContentId(websiteMenuContent.getId());
            }
            websiteContentImageService.insertOrUpdateAllColumnBatch(list);
        }
        redisTemplate.delete(fKey);
        return BaseResult.success();
    }


    /**
     * 新增/修改菜单内容
     *
     * @param websiteMenuContents
     * @return
     */
    @RequestMapping("/batchUpdate")
    public BaseResult batchUpdate(@RequestBody List<WebsiteMenuContent> websiteMenuContents) {
        try {
            for (int i = 0; i < websiteMenuContents.size(); i++) {
                WebsiteMenuContent websiteMenuContent = websiteMenuContents.get(i);
                insertMenuContent(websiteMenuContent);
            }

            redisTemplate.delete(fKey);
            return BaseResult.success();
        } catch (Exception e) {
            return BaseResult.fail("201", "操作失败请联系管理员");
        }

    }

    /**
     * 删除菜单图片内容
     */
    @RequestMapping("/deleteImage")
    public BaseResult deleteImage(@RequestBody WebsiteContentImage websiteContentImage) {
        try {
            WebsiteContentImage image = new WebsiteContentImage();
            image.setContentId(websiteContentImage.getContentId());
            EntityWrapper<WebsiteContentImage> imageWrapper = new EntityWrapper<>(image);
            List<WebsiteContentImage> websiteContentImages = websiteContentImageService.selectList(imageWrapper);
            if (websiteContentImages.size() <= 1 || websiteContentImages.size() > 10) {
                return BaseResult.fail("轮播图不能小于1张或大于10张");
            }
            if (!StringUtils.isNull(websiteContentImage.getId())) {
                websiteContentImageService.deleteById(websiteContentImage.getId());
            }
            return BaseResult.success();
        } catch (Exception e) {
            log.error("删除图片失败，失败原因：[{}],[{}]", e.getMessage(), e);
            return BaseResult.fail("删除图片失败");
        }finally{
            redisTemplate.delete(fKey);
        }


    }

    /**
     * 菜单内容详情
     *
     * @param websiteMenuContent
     * @return
     */
    @RequestMapping("/findById")
    public BaseResult findById(@RequestBody WebsiteMenuContent websiteMenuContent) {
        String key = "7CC4A94F1F58D46A40E013E6FDF7E61A"+JSON.toJSONString(websiteMenuContent);
        Object getObject = redisTemplate.boundHashOps(fKey).get(key);
        if (null != getObject) {
            WebsiteMenuContent wmc =  JSON.parseObject(getObject.toString(), WebsiteMenuContent.class);
            return BaseResult.successWithData(wmc);
        }
        EntityWrapper<WebsiteMenuContent> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(websiteMenuContent);
        WebsiteMenuContent wmc = websiteMenuContentService.selectOne(entityWrapper);
        EntityWrapper<WebsiteContentImage> websiteContentImageEntityWrapper = new EntityWrapper<>();
        WebsiteContentImage websiteContentImage = new WebsiteContentImage();
        websiteContentImage.setContentId(wmc.getId());
        List<WebsiteContentImage> websiteContentImages = websiteContentImageService.selectList(websiteContentImageEntityWrapper);
        wmc.setList(websiteContentImages);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(wmc));
        return BaseResult.successWithData(wmc);
    }


    /**
     * 菜单内容详情列表
     *
     * @param websiteMenuContent
     * @return
     */
    @RequestMapping("/queryList")
    public BaseResult queryList(@RequestBody WebsiteMenuContent websiteMenuContent) {
        String key = "A97BBFC790057AD23233A92A4BF7D676"+JSON.toJSONString(websiteMenuContent);
        Object getObject = redisTemplate.boundHashOps(fKey).get(key);
//        if (null != getObject) {
//            List<WebsiteMenuContent> websiteMenuContents = JSON.parseArray(getObject.toString(), WebsiteMenuContent.class);
//            return BaseResult.successWithData(websiteMenuContents);
//        }
        EntityWrapper<WebsiteMenuContent> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(websiteMenuContent);
        List<WebsiteMenuContent> websiteMenuContents = websiteMenuContentService.selectList(entityWrapper);
        for (WebsiteMenuContent wmc : websiteMenuContents) {
            EntityWrapper<WebsiteContentImage> websiteContentImageEntityWrapper = new EntityWrapper<>();
            WebsiteContentImage websiteContentImage = new WebsiteContentImage();
            websiteContentImage.setContentId(wmc.getId());
            websiteContentImageEntityWrapper.setEntity(websiteContentImage);
            List<WebsiteContentImage> websiteContentImages = websiteContentImageService.selectList(websiteContentImageEntityWrapper);
            wmc.setList(websiteContentImages);
        }
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(websiteMenuContents));
        return BaseResult.successWithData(websiteMenuContents);
    }


    private void insertMenuContent(WebsiteMenuContent websiteMenuContent) {
        if (StringUtils.isNull(websiteMenuContent.getId())) {
            int contentId = websiteMenuContentService.insertMenuContent(websiteMenuContent);
            List<WebsiteContentImage> list = websiteMenuContent.getList();
            if (list != null && list.size() > 0) {
                for (WebsiteContentImage wci : list) {
                    wci.setContentId(contentId);
                }
                websiteContentImageService.insertOrUpdateAllColumnBatch(list);
            }
        } else {
            websiteMenuContentService.updateById(websiteMenuContent);
            List<WebsiteContentImage> list = websiteMenuContent.getList();
            if (list != null && list.size() > 0) {
                for (WebsiteContentImage wci : list) {
                    wci.setContentId(websiteMenuContent.getId());
                }
                websiteContentImageService.insertOrUpdateAllColumnBatch(list);
            }
        }
    }


    /**
     * 查询分立和被动器件封装图片
     */
    @RequestMapping("/findWebsiteContentImage")
    public BaseResult findWebsiteContentImage(@RequestBody WebsiteContentImage websiteContentImage) {

        String key = "157A374791BA447BD135"+JSON.toJSONString(websiteContentImage);
        Object getObject = redisTemplate.boundHashOps(fKey).get(key);
        if (null != getObject) {
            List<WebsiteContentImage> websiteContentImages =JSON.parseArray(getObject.toString(), WebsiteContentImage.class);
            return BaseResult.successWithData(websiteContentImages);
        }

        EntityWrapper<WebsiteContentImage> entityWrapper = new EntityWrapper<>();
        entityWrapper.setEntity(websiteContentImage);
        List<WebsiteContentImage> websiteContentImages = websiteContentImageService.selectList(entityWrapper);
        redisTemplate.boundHashOps(fKey).put(key, JSON.toJSONString(websiteContentImages));
        return BaseResult.successWithData(websiteContentImages);
    }
}
