package com.ltgk.web.controller.system;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ltgk.common.annotation.DataScope;
import com.ltgk.common.annotation.Log;
import com.ltgk.common.core.controller.BaseController;
import com.ltgk.common.core.domain.AjaxResult;
import com.ltgk.common.core.domain.entity.SysUser;
import com.ltgk.common.core.domain.model.LoginUser;
import com.ltgk.common.core.page.TableDataInfo;
import com.ltgk.common.enums.BusinessType;
import com.ltgk.common.utils.SecurityUtils;
import com.ltgk.common.utils.StringUtils;
import com.ltgk.common.utils.poi.ExcelUtil;
import com.ltgk.common.validation.groups.Add;
import com.ltgk.common.validation.groups.Detail;
import com.ltgk.common.validation.groups.Update;
import com.ltgk.system.domain.OpinionManagement;
import com.ltgk.system.service.IOpinionManagementService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.List;

/**
 * 意见管理 (OpinionManagement) Controller控制器
 *
 * @author Qi ChengBin
 * @date 2025-03-20
 */
@RestController
@RequestMapping("/system/opinionManagement")
@Api(tags = "意见管理")
public class OpinionManagementController extends BaseController {

    @Resource
    private IOpinionManagementService opinionManagementService;

    /**
     * 分页查询意见管理列表
     * 接口权限: system:opinionManagement:page
     *
     * @param req: 意见管理 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    @PostMapping("/page")
    @ApiOperation(value = "分页查询意见管理列表")
    @DataScope
    public TableDataInfo page(@RequestBody OpinionManagement req) {
        SysUser currentUser = SecurityUtils.getLoginUser().getUser();
        LambdaQueryWrapper<OpinionManagement> wrapper = getLambdaQueryWrapperByReq(req);

        // 不是管理员查询当前登录用户的数据
        if (StringUtils.isNotNull(currentUser) && !currentUser.isAdmin()) {
            wrapper.eq(OpinionManagement::getUserId, currentUser.getUserId());
        }

        startPage(req.getPageNum(), req.getPageSize());
        List<OpinionManagement> list = opinionManagementService.list(wrapper);
        return getDataTable(list);
    }
    /**
     * 新增意见管理数据
     * 接口权限: system:opinionManagement:add
     *
     * @param opinionManagement: 意见管理实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    @Log(title = "新增意见管理数据", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ApiOperation(value = "新增意见管理数据")
    public AjaxResult add(@Validated(Add.class) @RequestBody OpinionManagement opinionManagement) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        String userId = String.valueOf(loginUser.getUserId());
        String userName = loginUser.getUsername();
        String nickName = loginUser.getUser().getNickName();
        Date nowDate = new Date();

        opinionManagement
                .setUserId(userId)
                .setUserName(userName)
                .setNickName(nickName)
                .setPublishTime(nowDate)
                .setCreateBy(nickName)
                .setCreateAt(nowDate)
                .setUpdateBy(nickName)
                .setUpdateAt(nowDate);
        return toAjax(opinionManagementService.save(opinionManagement));
    }

    /**
     * 修改意见管理数据
     * 接口权限: system:opinionManagement:edit
     *
     * @param opinionManagement: 意见管理实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    @PreAuthorize("@ss.hasPermi('system:opinionManagement:edit')")
    @Log(title = "修改意见管理", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ApiOperation(value = "修改意见管理数据")
    public AjaxResult edit(@Validated(Update.class) @RequestBody OpinionManagement opinionManagement) {
        String userName = SecurityUtils.getLoginUser().getUser().getNickName();
        opinionManagement.setUpdateBy(userName)
                .setUpdateAt(new Date());
        return toAjax(opinionManagementService.updateById(opinionManagement));
    }

    /**
     * 删除意见管理数据
     * 接口权限: system:opinionManagement:remove
     *
     * @param req: 意见管理 实体对象
     * @return com.ltgk.common.core.domain.AjaxResult
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    @PreAuthorize("@ss.hasPermi('system:opinionManagement:remove')")
    @Log(title = "删除意见管理数据", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    @ApiOperation(value = "删除意见管理数据")
    public AjaxResult delete(@Validated(Detail.class) @RequestBody OpinionManagement req) {
        return toAjax(opinionManagementService.removeById(req.getId()));
    }

    /**
     * 导出意见管理数据
     * 接口权限: system:opinionManagement:export
     *
     * @param response:
     * @param req:      意见管理 实体对象
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    @PreAuthorize("@ss.hasPermi('system:opinionManagement:export')")
    @Log(title = "导出意见管理数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ApiOperation(value = "导出意见管理数据")
    public void export(HttpServletResponse response, OpinionManagement req) {
        LambdaQueryWrapper<OpinionManagement> wrapper = getLambdaQueryWrapperByReq(req);
        List<OpinionManagement> list = opinionManagementService.list(wrapper);
        ExcelUtil<OpinionManagement> util = new ExcelUtil<>(OpinionManagement.class);
        util.exportExcel(response, list, "意见管理数据");
    }

    /**
     * 根据请求参数转换成 LambdaQueryWrapper
     *
     * @param req: 意见管理 实体对象
     * @return com.ltgk.common.core.page.TableDataInfo
     * @author Qi ChengBin
     * @date 2025-03-20
     */
    private static LambdaQueryWrapper<OpinionManagement> getLambdaQueryWrapperByReq(OpinionManagement req) {
        req.queryCheck();
        String startTime = req.getStartTime();
        String endTime = req.getEndTime();
        boolean beNotBlankTime = StringUtils.isNotBlank(startTime) && StringUtils.isNotBlank(endTime);
        return new LambdaQueryWrapper<>(req)
                .between(beNotBlankTime, OpinionManagement::getUpdateAt, startTime, endTime);
    }
}

