package com.mosty.common.core.business.controller;


import com.baomidou.mybatisplus.core.metadata.IPage;
import com.mosty.common.base.domain.ResponseResult;
import com.mosty.common.base.entity.log.BusinessType;
import com.mosty.common.base.entity.log.Log;
import com.mosty.common.base.util.IpUtil;
import com.mosty.common.core.business.entity.SysDept;
import com.mosty.common.core.business.entity.SysUserAppHomeconfigPlateList;
import com.mosty.common.core.business.entity.request.SysUserAppHomeconfigPlateListPage;
import com.mosty.common.core.business.service.SysUserAppHomeconfigPlateListService;
import com.mosty.common.token.JwtSysUser;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.util.List;

/**
 * <p>
 * 用户app首页配置-板块内容表 前端控制器
 * </p>
 *
 * @author zengbo
 * @since 2022-08-18
 */
@Api(tags = "app用户首页板块内容接口")
@RestController
@AllArgsConstructor
@RequestMapping("/sysUserAppHomeconfigPlateList")
public class SysUserAppHomeconfigPlateListController {

    private SysUserAppHomeconfigPlateListService sysUserAppHomeconfigPlateListService;

    /**
     * 添加用户首页应用板块内容
     * @param config
     * @return 添加结果
     */
    @PostMapping("add")
    @JwtSysUser
    @ApiOperation(value = "添加用户首页应用板块内容", httpMethod = "POST", response = Boolean.class)
    @Log(title = "添加首页应用板块内容",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> add(@RequestBody @Valid SysUserAppHomeconfigPlateList config,
                                       HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigPlateListService.saveInfo(config, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 添加用户首页应用内容
     * @param plates
     * @return 添加结果
     */
    @PostMapping("addBatch")
    @JwtSysUser
    @ApiOperation(value = "批量添加用户首页应用板块内容", httpMethod = "POST", response = Boolean.class)
    @Log(title = "批量添加首页应用板块内容",businessType = BusinessType.INSERT)
    public ResponseResult<Boolean> addBatch(@RequestBody List<SysUserAppHomeconfigPlateList> plates,
                                            HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigPlateListService.saveInfoBatch(plates, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 修改用户首页应用
     * @param plate 修改用户首页应用信息
     * @return 修改结果
     */
    @PostMapping("update")
    @JwtSysUser
    @Log(title = "首页应用板块内容",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "修改用户首页应用板块内容", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> update(@RequestBody @Valid SysUserAppHomeconfigPlateList plate,
                                          HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigPlateListService.updateInfo(plate, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 批量修改用户首页应用板块
     * @param plates 首页板块信息
     * @return 修改结果
     */
    @PostMapping("updateBatch")
    @JwtSysUser
    @Log(title = "批量修改用户首页应用板块内容",businessType = BusinessType.UPDATE)
    @ApiOperation(value = "批量修改用户首页应用板块内容", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> updateBatch(@RequestBody List<SysUserAppHomeconfigPlateList> plates, HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
        sysUserAppHomeconfigPlateListService.updateInfoBatch(plates, ipAddress);
        return ResponseResult.success(Boolean.TRUE);
    }

    /**
     * 删除应用板块信息
     * @param list 应用id
     * @return 删除结果
     */
    @Log(title = "删除用户应用板块内容功能",businessType = BusinessType.DELETE)
    @PostMapping("deletePlate")
    @JwtSysUser
    @ApiOperation(value = "删除用户应用板块内容信息", httpMethod = "POST", response = Boolean.class)
    public ResponseResult<Boolean> deletePlate(@RequestBody List<Long> list,
                                               HttpServletRequest servletRequest) {
        String ipAddress = IpUtil.getIpAddress(servletRequest);
//        UserInfo userInfo = UserInfoManager.get();
        sysUserAppHomeconfigPlateListService.delete(list, ipAddress, null);
        return ResponseResult.success(Boolean.TRUE);
    }

    @GetMapping("selectPage")
    @ApiOperation(value = "分页查询应用板块内容信息", httpMethod = "GET", response = SysDept.class)
    public ResponseResult<IPage<SysUserAppHomeconfigPlateList>> selectPage(SysUserAppHomeconfigPlateListPage deptPage) {
        IPage<SysUserAppHomeconfigPlateList> deptIPage = sysUserAppHomeconfigPlateListService.selectByPage(deptPage);
        return ResponseResult.success(deptIPage);
    }

}

