package com.ruoyi.xq.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.helper.LoginHelper;
import com.ruoyi.common.utils.ServletUtils;
import com.ruoyi.xq.domain.User;
import com.ruoyi.xq.domain.UserPictures;
import com.ruoyi.xq.dto.admin.common.AuditFailReq;
import com.ruoyi.xq.dto.admin.user.UserPicturesAdminMergeVo;
import com.ruoyi.xq.dto.admin.user.UserPicturesAdminVo;
import com.ruoyi.xq.dto.admin.user.req.UserPicturesBatchUpdateReq;
import com.ruoyi.xq.dto.app.common.IdsReq;
import com.ruoyi.xq.enums.common.AuditEnum;
import com.ruoyi.xq.service.UserPicturesService;
import com.ruoyi.xq.service.UserService;
import lombok.RequiredArgsConstructor;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * 用户相册管理
 *
 * @author 77
 * @date 2024-03-04
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/xq/userPictures")
public class UserPicturesController extends BaseController {

    private final UserPicturesService userPicturesService;
    private final UserService userService;

    /**
     * 查询用户相册管理列表
     */
    @SaCheckPermission("xq:userPictures:list")
    @GetMapping("/list")
    public TableDataInfo<UserPicturesAdminVo> list(UserPicturesAdminVo bo, PageQuery pageQuery) {
        Page<UserPicturesAdminVo> page = userPicturesService.pageAdmin(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    @SaCheckPermission("xq:userPictures:merge")
    @GetMapping("/list/merge")
    public TableDataInfo<UserPicturesAdminMergeVo> listMerge(UserPicturesAdminMergeVo bo, PageQuery pageQuery) {
        Page<UserPicturesAdminMergeVo> page = userPicturesService.pageAdminManager(pageQuery, bo);
        return TableDataInfo.build(page);
    }

    /**
     * 获取用户相册管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("xq:userPictures:query")
    @GetMapping("/{id}")
    public R<UserPictures> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(userPicturesService.getById(id));
    }

    @GetMapping("/listByUserId")
    public R<List<UserPictures>> listByUserId(Long userId){
        List<UserPictures> userPicturesList = userPicturesService.listByUserIdAuditingAndSuccess(userId);
        return R.ok(userPicturesList);
    }


    @PostMapping("/batchUpdate")
    @Log(title = "用户相册批量修改", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    public R<Void> listByUserId(@RequestBody UserPicturesBatchUpdateReq req){
        userPicturesService.batchUpdate(req.getUserId(),req.getUserPictures());
        return R.ok();
    }

    /**
     * 新增用户相册管理
     */
    @SaCheckPermission("xq:userPictures:add")
    @Log(title = "用户相册管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> addOrUpdate(@Validated(AddGroup.class) @RequestBody UserPictures bo) {
        User user = userService.getByUsercode(bo.getUsercode());
        if(user == null){
            return R.fail("用户不存在");
        }
        String[] split = bo.getPicture().split(",");
        for (String picture : split) {
            UserPictures pictures = new UserPictures();
            pictures.setUserId(user.getId());
            pictures.setUsercode(user.getUsercode());
            pictures.setPicture(picture);
            pictures.setAuditStatus(AuditEnum.SUCCESS.getCode());
            pictures.setAuditRemark("后台新增免审核");
            pictures.setAuditTime(LocalDateTime.now());
            pictures.setAuditOpId(LoginHelper.getUserId());
            pictures.setAuditOpName(LoginHelper.getUsername());
            pictures.setAuditIp(ServletUtils.getClientIP());
            userPicturesService.save(pictures);
        }
        return R.ok();
    }

    @SaCheckPermission("xq:userPictures:edit")
    @Log(title = "用户相册审核成功", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/audit/success")
    public R<Void> auditSuccess(@RequestBody IdsReq bo) {
        userPicturesService.auditSuccess(bo.getIds());
        return R.ok();
    }

    @SaCheckPermission("xq:userPictures:edit")
    @Log(title = "用户相册审核失败", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PostMapping("/audit/fail")
    public R<Void> auditFail(@RequestBody AuditFailReq bo) {
        userPicturesService.auditFail(bo.getId(),bo.getAuditRemark());
        return R.ok();
    }

    /**
     * 删除用户相册管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("xq:userPictures:remove")
    @Log(title = "用户相册管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(userPicturesService.removeBatchByIds(Arrays.asList(ids)));
    }
}
