package com.storlead.sales.controller.customer;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.storlead.framework.util.LoginUserUtil;
import com.storlead.framework.auth.vo.LoginUser;
import com.storlead.framework.common.result.Result;
import com.storlead.sales.pojo.dto.customer.BusinessDeleteDTO;
import com.storlead.sales.pojo.dto.customer.SysResourceSearchDTO;
import com.storlead.sales.pojo.entity.customer.SysResourceEntity;
import com.storlead.sales.pojo.vo.customer.SysResourceListVO;
import com.storlead.sales.pojo.vo.customer.SysResourceVO;
import com.storlead.sales.service.customer.SysResourceService;
import com.storlead.system.pojo.entity.SystemOssConfigEntity;
import com.storlead.system.service.SystemOssConfigService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import javax.annotation.Resource;
import org.springframework.util.CollectionUtils;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * <p>
 * 系统资源表 前端控制器
 * </p>
 *
 * @author YPZ
 * @since 2024-04-29
 */
@RestController
@RequestMapping("/sys-resource")
@Api(tags = "销售: 系统附件管理相关接口")
public class SysResourceController {
    @Resource
    private SysResourceService sysResourceService;
    @Resource
    private SystemOssConfigService ossConfigService;

    @GetMapping(value = "/list-none-page")
    @ApiOperation(value = "附件查询", notes = "附件查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SysResourceEntity.class)
    })
    public Result<?> searchDeptStaffList(SysResourceEntity sysResource) {
        List<SysResourceEntity> reuslt = sysResourceService.list(buildQueryWrapper(sysResource));
        return Result.result(reuslt);
    }

    @GetMapping(value = "/list")
    @ApiOperation(value = "附件查询", notes = "附件查询")
    @ApiResponses({
            @ApiResponse(code = 200, message = "", response = SysResourceEntity.class)
    })
    public Result<IPage<SysResourceListVO>> list(SysResourceSearchDTO sysResourceSearchDTO) {
        return sysResourceService.selectListVO(sysResourceSearchDTO);
    }

    /**
     * 添加
     *
     * @param sysResourceVO
     * @return
     */
    @PostMapping(value = "/add", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "资源相关接口-添加", notes = "资源相关接口-添加目标")
    public Result<?> add(SysResourceVO sysResourceVO) {

        LoginUser loginUser = LoginUserUtil.getLoginUser();
        sysResourceVO.setCreateBy(Long.parseLong(loginUser.getId()+""));
        Result result = sysResourceService.saveVo(sysResourceVO);
        return result;
    }

    /**
     * 添加
     *
     * @param sysResourceVO
     * @return
     */
    @PostMapping(value = "/add-list", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "资源相关接口-批量上传附件", notes = "资源相关接口-批量上传附件")
    public Result<?> addList(SysResourceVO sysResourceVO) {

        Result result = sysResourceService.saveFileList(sysResourceVO);
        return result;
    }

    /**
     * 添加
     *
     * @param sysResourceVO
     * @return
     */
    @PostMapping(value = "/upload-list", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "资源相关接口-批量上传附件", notes = "资源相关接口-批量上传附件")
    public Result<?> uploadList(SysResourceVO sysResourceVO) {
        Result result = sysResourceService.uploadFileList(sysResourceVO);
        return result;
    }

    @PostMapping(value = "/upload-list-material", headers = "content-type=multipart/form-data")
    @ApiOperation(value = "资源相关接口-批量上传附件", notes = "资源相关接口-批量上传附件")
    public Result<?> uploadListMaterial(SysResourceVO sysResourceVO) {
        SystemOssConfigEntity systemOssConfig = ossConfigService.getById(2);
        Result result = sysResourceService.saveVo(sysResourceVO,systemOssConfig.getBucketName(),systemOssConfig.getEndPoint());
        return result;
    }

//    @PostMapping(value = "/upload-list-Res", headers = "content-type=multipart/form-data")
//    @ApiOperation(value = "资源相关接口-批量上传附件", notes = "资源相关接口-批量上传附件")
//    public Result<?> uploadListRes(SysResourceVO sysResourceVO) {
//
//        Result result = sysResourceService.uploadFileList(sysResourceVO);
//        return result;
//    }


    @PostMapping(value = "/add-resource")
    @ApiOperation(value = "资源相关接口-批量绑定资源", notes = "资源相关接口-批量绑定资源")
    public Result<?> addResource(SysResourceVO sysResourceVO) {

        Result result = sysResourceService.addResourceByIdList(sysResourceVO);
        return result;
    }


//    @ApiOperation("删除附件资源")
//    @DeleteMapping("/delete")
//    public Result remove(List<Long> ids) {
//        if (CollectionUtils.isEmpty(ids)) {
//            return Result.error("参数错误");
//        }
//        List<SysResourceEntity> resourceList = new ArrayList<>();
//        for (Long id : ids){
//            SysResourceEntity resource = new SysResourceEntity();
//            resource.setId(id);
//            resource.setIsDelete(CommonConstant.DEL_FLAG_1);
//            resourceList.add(resource);
//        }
//        boolean isOk = sysResourceService.saveOrUpdateBatch(resourceList);
//        if (isOk) {
//            return Result.ok();
//        }
//        return Result.error("删除失败");
//    }

    @ApiOperation("删除附件资源")
    @DeleteMapping("/delete-by-id")
    public Result remove(Long id) {
        if (Objects.isNull(id)) {
            return Result.error("id不能为空");
        }
        sysResourceService.lgDelete(id);
        return Result.ok();
    }

    @ApiOperation("删除附件资源")
    @DeleteMapping("/delete")
    public Result delete(@RequestBody BusinessDeleteDTO businessDeleteDTO) {
        boolean deleteOk = false;
        if(!ObjectUtils.isEmpty(businessDeleteDTO.getId())) {
            deleteOk = sysResourceService.lgDelete(businessDeleteDTO.getId());
        }
        if(!CollectionUtils.isEmpty(businessDeleteDTO.getIds())) {
            deleteOk = sysResourceService.lgDelete(businessDeleteDTO.getIds());
        }
        if(!deleteOk) {
            return Result.error("删除失败");
        }
        return Result.ok();
    }

    private LambdaQueryWrapper<SysResourceEntity> buildQueryWrapper(SysResourceEntity po) {
        LambdaQueryWrapper<SysResourceEntity> lqw = Wrappers.lambdaQuery();
        lqw.eq(po.getId() != null, SysResourceEntity::getId, po.getId());
        lqw.eq(po.getCustomerId() != null, SysResourceEntity::getCustomerId, po.getCustomerId());
        lqw.eq(po.getResourceType() != null, SysResourceEntity::getResourceType, po.getResourceType());
        lqw.eq(po.getResourceTypeId() != null, SysResourceEntity::getResourceTypeId, po.getResourceTypeId());
        lqw.eq(StringUtils.isNotEmpty(po.getResourceName()) , SysResourceEntity::getResourceName, po.getResourceName());
        lqw.eq(StringUtils.isNotEmpty(po.getResourceSize() ), SysResourceEntity::getResourceSize, po.getResourceSize());
        lqw.eq(StringUtils.isNotEmpty(po.getResourceExt()), SysResourceEntity::getResourceExt, po.getResourceExt());
        lqw.eq(StringUtils.isNotEmpty(po.getResourceUrl()), SysResourceEntity::getResourceUrl, po.getResourceUrl());
        lqw.eq(po.getCreateBy() != null, SysResourceEntity::getCreateBy, po.getCreateBy());
        lqw.eq(po.getUpdateBy() != null, SysResourceEntity::getUpdateBy, po.getUpdateBy());
        return lqw;
    }
}
