package com.slodon.b2b2c.controller.system.admin;

import com.slodon.b2b2c.aop.OperationLogger;
import com.slodon.b2b2c.core.constant.RedisConst;
import com.slodon.b2b2c.core.constant.TplPcConst;
import com.slodon.b2b2c.core.controller.BaseController;
import com.slodon.b2b2c.core.response.JsonResult;
import com.slodon.b2b2c.core.response.PageVO;
import com.slodon.b2b2c.core.response.PagerInfo;
import com.slodon.b2b2c.core.response.SldResponse;
import com.slodon.b2b2c.core.util.AssertUtil;
import com.slodon.b2b2c.core.util.StringUtil;
import com.slodon.b2b2c.core.util.UserUtil;
import com.slodon.b2b2c.core.util.WebUtil;
import com.slodon.b2b2c.model.system.TplPcMallDataModel;
import com.slodon.b2b2c.model.system.TplPcMallDecoModel;
import com.slodon.b2b2c.system.example.TplPcMallDecoExample;
import com.slodon.b2b2c.system.pojo.Admin;
import com.slodon.b2b2c.system.pojo.TplPcMallDeco;
import com.slodon.b2b2c.vo.system.TplPcMallDecoDetailVO;
import com.slodon.b2b2c.vo.system.TplPcMallDecoVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;

@Api(tags = "admin-pc首页装修管理")
@RestController
@RequestMapping("v3/system/admin/pcDeco")
public class AdminPcDecoController extends BaseController {

    @Resource
    private TplPcMallDecoModel tplPcMallDecoModel;
    @Resource
    private TplPcMallDataModel tplPcMallDataModel;
    @Resource
    private StringRedisTemplate stringRedisTemplate;

    @ApiOperation("首页装修列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点", paramType = "query"),
            @ApiImplicitParam(name = "decoType", value = "装修页类型", paramType = "query"),
            @ApiImplicitParam(name = "decoName", value = "装修页名称", paramType = "query"),
            @ApiImplicitParam(name = "isEnable", value = "是否启用，0==不启用；1==启用", paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "分页大小", defaultValue = "20", paramType = "query"),
            @ApiImplicitParam(name = "current", value = "当前页面位置", defaultValue = "1", paramType = "query")
    })
    @GetMapping("list")
    public JsonResult<PageVO<TplPcMallDecoVO>> list(HttpServletRequest request,
                                                    @RequestParam(value = "webSite", required = false)  String webSite,
                                                    @RequestParam(value = "decoType", required = false) String decoType,
                                                    @RequestParam(value = "decoName", required = false) String decoName,
                                                    @RequestParam(value = "isEnable", required = false) Integer isEnable) {
        PagerInfo pager = WebUtil.handlerPagerInfo(request);
        TplPcMallDecoExample example = new TplPcMallDecoExample();
        example.setWebSite(webSite);
        example.setDecoType(decoType);
        example.setDecoNameLike(decoName);
        example.setIsEnable(isEnable);
        example.setStoreId(0L);
        List<TplPcMallDeco> list = tplPcMallDecoModel.getTplPcMallDecoList(example, pager);
        List<TplPcMallDecoVO> vos = new ArrayList<>();
        if (!CollectionUtils.isEmpty(list)) {
            list.forEach(deco -> {
                vos.add(new TplPcMallDecoVO(deco));
            });
        }
        List<TplPcMallDecoVO> vos1;
        vos1 = vos.stream().filter(e -> e.getDecoId()!=34).collect(Collectors.toList());
        return SldResponse.success(new PageVO<>(vos, pager));
    }

    @ApiOperation("装修页详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decoId", value = "装修页id", required = true)
    })
    @GetMapping("display")
    public JsonResult<TplPcMallDecoDetailVO> display(Integer decoId) {
        TplPcMallDeco tplPcMallDeco = tplPcMallDecoModel.getTplPcMallDecoByDecoId(decoId);
        return SldResponse.success(new TplPcMallDecoDetailVO(tplPcMallDeco));
    }

    @ApiOperation("添加装修页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "webSite", value = "站点"),
            @ApiImplicitParam(name = "decoType", value = "装修页类型", required = true),
            @ApiImplicitParam(name = "decoName", value = "装修页名称", required = true)
    })
    @OperationLogger(option = "添加装修页")
    @PostMapping("add")
    public JsonResult add(HttpServletRequest request,String webSite, String decoType, String decoName) {
        Admin adminUser = UserUtil.getUser(request, Admin.class);

        TplPcMallDeco tplPcMallDeco = new TplPcMallDeco();
        tplPcMallDeco.setWebSite(webSite);
        tplPcMallDeco.setDecoType(decoType);
        tplPcMallDeco.setDecoName(decoName);
        tplPcMallDeco.setIsEnable(TplPcConst.IS_ENABLE_NO);
        tplPcMallDeco.setCreateUserId(adminUser.getAdminId().longValue());
        tplPcMallDeco.setCreateUserName(adminUser.getAdminName());
        tplPcMallDeco.setCreateTime(new Date());
        tplPcMallDeco.setUpdateUserId(adminUser.getAdminId().longValue());
        tplPcMallDeco.setUpdateUserName(adminUser.getAdminName());
        tplPcMallDeco.setUpdateTime(new Date());
        tplPcMallDeco.setStoreId(0L);
        tplPcMallDecoModel.saveTplPcMallDeco(tplPcMallDeco,false);
        return SldResponse.success("添加成功", "装修页名称:" + tplPcMallDeco.getDecoName());
    }

    @ApiOperation("编辑装修页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decoId", value = "装修页id", required = true),
            @ApiImplicitParam(name = "decoType", value = "装修页类型"),
            @ApiImplicitParam(name = "decoName", value = "装修页名称"),
            @ApiImplicitParam(name = "data", value = "装修页数据"),
            @ApiImplicitParam(name = "isEnable", value = "是否启用该装修页；0==不启用，1==启用")
    })
    @OperationLogger(option = "编辑装修页")
    @PostMapping("update")
    public JsonResult update(HttpServletRequest request, Integer decoId, String decoType, String decoName, String data, Integer isEnable) {
        Admin adminUser = UserUtil.getUser(request, Admin.class);

        TplPcMallDeco tplPcMallDecoDb = tplPcMallDecoModel.getTplPcMallDecoByDecoId(decoId);
        AssertUtil.notNull(tplPcMallDecoDb, "该装修页不存在");

        TplPcMallDeco tplPcMallDeco = new TplPcMallDeco();
        tplPcMallDeco.setDecoId(decoId);
        tplPcMallDeco.setWebSite(tplPcMallDecoDb.getWebSite());
        tplPcMallDeco.setDecoType(decoType);
        tplPcMallDeco.setDecoName(decoName);
        tplPcMallDeco.setData(data);
        tplPcMallDeco.setIsEnable(isEnable);
        tplPcMallDeco.setStoreId(0L);
        tplPcMallDeco.setUpdateUserId(adminUser.getAdminId().longValue());
        tplPcMallDeco.setUpdateUserName(adminUser.getAdminName());
        tplPcMallDeco.setUpdateTime(new Date());
        tplPcMallDecoModel.updateTplPcMallDeco(tplPcMallDeco);

        //本身未开启并且修改为开启时执行此操作
        if (!StringUtil.isNullOrZero(isEnable) && isEnable == TplPcConst.IS_ENABLE_YES
                && tplPcMallDecoDb.getIsEnable() == TplPcConst.IS_ENABLE_NO) {
            //关闭其他开启的装修页
            TplPcMallDecoExample example = new TplPcMallDecoExample();
            example.setDecoIdNotEquals(decoId);
            example.setWebSite(tplPcMallDecoDb.getWebSite());
            example.setDecoType(tplPcMallDecoDb.getDecoType());
            example.setIsEnable(TplPcConst.IS_ENABLE_YES);
            example.setStoreId(0L);
            TplPcMallDeco disableDeco = new TplPcMallDeco();
            disableDeco.setIsEnable(TplPcConst.IS_ENABLE_NO);
            tplPcMallDecoModel.updateTplPcMallDecoByExample(disableDeco, example);
        }

        //如果装修页类型是首页，并且开启
        if ("index".equals(tplPcMallDecoDb.getDecoType()) && !StringUtil.isNullOrZero(isEnable) && isEnable == TplPcConst.IS_ENABLE_YES) {
            if (!StringUtil.isEmpty(data)) {
                //如果不为空则存入redis
                stringRedisTemplate.opsForValue().set(RedisConst.PC_INDEX + "_" + tplPcMallDecoDb.getWebSite(), data);
            } else {
                //如果为空则删除redis
                stringRedisTemplate.delete(RedisConst.PC_INDEX + "_" + tplPcMallDecoDb.getWebSite());
            }
        }
        return SldResponse.success("修改成功", "装修页ID:" + tplPcMallDeco.getDecoId());
    }

    @ApiOperation("删除装修页")
    @OperationLogger(option = "删除装修页")
    @PostMapping("del")
    public JsonResult delTplData(HttpServletRequest request, Integer decoId) {
        tplPcMallDecoModel.deleteTplPcMallDeco(decoId, 0L);
        return SldResponse.success("删除成功", "装修页ID:" + decoId);
    }

    @ApiOperation("启/停用装修页")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "decoId", value = "装修页id", required = true),
            @ApiImplicitParam(name = "isEnable", value = "启/停用[true==启用,false==停用]", required = true)
    })
    @OperationLogger(option = "启/停用装修页")
    @PostMapping("isEnable")
    public JsonResult isEnableDeco(HttpServletRequest request, Integer decoId, Boolean isEnable) {
        TplPcMallDeco tplPcMallDecoDb = tplPcMallDecoModel.getTplPcMallDecoByDecoId(decoId);
        AssertUtil.notNull(tplPcMallDecoDb, "该装修页不存在，请新选择");

        TplPcMallDeco tplPcMallDeco = new TplPcMallDeco();
        tplPcMallDeco.setDecoId(decoId);
        if (isEnable) {
            if (tplPcMallDecoDb.getDecoType().contains("index")) {
                //关闭其他开启的装修页
                TplPcMallDecoExample example = new TplPcMallDecoExample();
                example.setWebSite(tplPcMallDecoDb.getWebSite());
                example.setDecoType(tplPcMallDecoDb.getDecoType());
                example.setIsEnable(TplPcConst.IS_ENABLE_YES);
                example.setStoreId(0L);
                TplPcMallDeco disableDeco = new TplPcMallDeco();
                disableDeco.setIsEnable(TplPcConst.IS_ENABLE_NO);
                tplPcMallDecoModel.updateTplPcMallDecoByExample(disableDeco, example);
            }
            tplPcMallDeco.setIsEnable(TplPcConst.IS_ENABLE_YES);
            tplPcMallDecoModel.updateTplPcMallDeco(tplPcMallDeco);

            //如果装修页类型是首页，存入redis
            if ("index".equals(tplPcMallDecoDb.getDecoType())) {
                String data = tplPcMallDecoDb.getData();
                if (!StringUtil.isEmpty(data)) {
                    //如果不为空则存入redis
                    stringRedisTemplate.opsForValue().set(RedisConst.PC_INDEX + "_" + tplPcMallDecoDb.getWebSite(), data);
                } else {
                    //如果为空则删除redis
                    stringRedisTemplate.delete(RedisConst.PC_INDEX + "_" + tplPcMallDecoDb.getWebSite());
                }
            }

            return SldResponse.success("启用成功", "装修页ID:" + decoId);
        } else {
            tplPcMallDeco.setIsEnable(TplPcConst.IS_ENABLE_NO);
            tplPcMallDecoModel.updateTplPcMallDeco(tplPcMallDeco);

            //如果装修页类型是首页，删除redis缓存数据
            if ("index".equals(tplPcMallDecoDb.getDecoType())) {
                stringRedisTemplate.delete(RedisConst.PC_INDEX + "_" + tplPcMallDecoDb.getWebSite());
            }

            return SldResponse.success("停用成功", "装修页ID:" + decoId);
        }
    }

    @ApiOperation("复制装修页")
    @OperationLogger(option = "复制装修页")
    @PostMapping("copy")
    public JsonResult copyDeco(HttpServletRequest request, @RequestParam(value = "decoId") Integer decoId) {
        Admin adminUser = UserUtil.getUser(request, Admin.class);

        TplPcMallDeco tplPcMallDecoDb = tplPcMallDecoModel.getTplPcMallDecoByDecoId(decoId);
        AssertUtil.notNull(tplPcMallDecoDb, "该装修页不存在，请新选择");

        TplPcMallDeco deco = new TplPcMallDeco();
        deco.setWebSite(tplPcMallDecoDb.getWebSite());
        deco.setDecoType(tplPcMallDecoDb.getDecoType());
        deco.setDecoName("copy-" + tplPcMallDecoDb.getDecoName());
        deco.setData(tplPcMallDecoDb.getData());
        deco.setIsEnable(TplPcConst.IS_ENABLE_NO);
        deco.setCreateUserId(adminUser.getAdminId().longValue());
        deco.setCreateUserName(adminUser.getAdminName());
        deco.setCreateTime(new Date());
        deco.setUpdateUserId(adminUser.getAdminId().longValue());
        deco.setUpdateUserName(adminUser.getAdminName());
        deco.setUpdateTime(new Date());
        deco.setStoreId(0L);
        tplPcMallDecoModel.saveTplPcMallDeco(deco,true);
        return SldResponse.success("复制成功");
    }

}
