package com.yunzhi.marketing.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.yunzhi.marketing.base.BaseController;
import com.yunzhi.marketing.base.ResponseBean;
import com.yunzhi.marketing.common.CommConstant;
import com.yunzhi.marketing.entity.TaMiniappTheme;
import com.yunzhi.marketing.service.ITaMiniappThemeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.time.LocalDateTime;

@RestController
@RequestMapping("/api")
public class TaMiniappThemeController extends BaseController {

    @Autowired
    ITaMiniappThemeService iTaMiniappThemeService;

    @GetMapping("/admin/miniapp-theme")
    public ResponseBean getThemeListForAdmin(@RequestParam(value ="pageNum",defaultValue = "1") Integer pageNum,
                                             @RequestParam(value ="pageSize",defaultValue = "10") Integer pageSize,
                                             HttpServletRequest request) {
        Integer orgId = getOrgId(request);

        Boolean includeNotUsed = true;
        IPage<TaMiniappTheme> themeList = iTaMiniappThemeService.getThemeList(orgId, includeNotUsed, pageNum, pageSize);

        return ResponseBean.success(themeList);
    }

    @GetMapping("/admin/miniapp-theme/{id}")
    public ResponseBean getThemeById(@PathVariable Integer id) {
        if (id == null || id == 0) {
            ResponseBean.error("未找到主题设置", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        TaMiniappTheme taMiniappTheme = iTaMiniappThemeService.getProfileById(id);
        if (taMiniappTheme == null) {
            ResponseBean.error("未找到主题设置", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        return ResponseBean.success(taMiniappTheme);
    }

    @PostMapping("/admin/miniapp-theme")
    public ResponseBean saveTheme(@RequestBody TaMiniappTheme taMiniappTheme, HttpServletRequest request) {
        return this.editTheme(taMiniappTheme, request);
    }

    @PutMapping("/admin/miniapp-theme")
    public ResponseBean updateTheme(@RequestBody TaMiniappTheme taMiniappTheme, HttpServletRequest request) {
        return this.editTheme(taMiniappTheme, request);
    }

    private ResponseBean editTheme(TaMiniappTheme taMiniappTheme, HttpServletRequest request) {
        if (taMiniappTheme == null) {
            return ResponseBean.error("非法参数", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        Integer userId = getUserId(request);
        taMiniappTheme.setOrgId(orgId);
        taMiniappTheme.setModifyUser(userId);
        taMiniappTheme.setModifyDate(LocalDateTime.now());

        return iTaMiniappThemeService.editTheme(taMiniappTheme);
    }

    @DeleteMapping("/admin/miniapp-theme/{themeId}")
    public ResponseBean deleteTheme(@PathVariable Integer themeId, HttpServletRequest request) {
        if (themeId == null || themeId <= 0) {
            return ResponseBean.error("未找到删除内容", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer orgId = getOrgId(request);
        QueryWrapper<TaMiniappTheme> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("theme_id", themeId);
        TaMiniappTheme taMiniappTheme = iTaMiniappThemeService.getById(themeId);
        if (taMiniappTheme == null || taMiniappTheme.getStatus().equals(CommConstant.STATUS_DELETE)) {
            return ResponseBean.success("");
        }

        if (!orgId.equals(taMiniappTheme.getOrgId())) {
            return ResponseBean.error("无权删除该主题", ResponseBean.ERROR_ILLEGAL_PARAMS);
        }

        Integer userId = getUserId(request);
        taMiniappTheme.setModifyUser(userId);

        return iTaMiniappThemeService.deleteTheme(taMiniappTheme);
    }

}
