package cn.iocoder.yudao.module.appBase.controller;

import cn.hutool.core.io.FileUtil;
import cn.hutool.core.io.IoUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.security.core.LoginUser;
import cn.iocoder.yudao.framework.security.core.util.SecurityFrameworkUtils;
import cn.iocoder.yudao.module.appBase.entity.AppSwiper;
import cn.iocoder.yudao.module.appBase.service.IAppSwiperService;
import cn.iocoder.yudao.module.core.controller.BaseController;
import cn.iocoder.yudao.module.infra.controller.app.file.vo.AppFileUploadReqVO;
import cn.iocoder.yudao.module.infra.service.file.FileService;
import cn.iocoder.yudao.module.shop.entity.PartnerDetail;
import cn.iocoder.yudao.module.shop.entity.ShopMallPartners;
import cn.iocoder.yudao.module.shop.service.IShopMallPartnersService;
import cn.iocoder.yudao.module.system.dal.dataobject.user.AdminUserDO;
import cn.iocoder.yudao.module.system.service.user.AdminUserService;
import cn.iocoder.yudao.util.ExcelUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

/**
 * 【请填写功能名称】Controller
 *
 * @author ruoyi
 * @date 2023-08-01
 */
@RestController
@RequestMapping("/admin-api")
public class AppSwiperController extends BaseController
{
    @Autowired
    private IAppSwiperService appSwiperService;
    @Autowired
    private IShopMallPartnersService partnersService;
    @Autowired
    private AdminUserService userService;

    @Resource
    private FileService fileService;

    @PostMapping({"/common/uploadImg","/common/upload"})
    @Operation(summary = "上传文件")
    public CommonResult uploadFile(AppFileUploadReqVO uploadReqVO) throws Exception {
        MultipartFile file = uploadReqVO.getFile();
        String path = uploadReqVO.getPath();
        String url = fileService.createFile(file.getOriginalFilename(), path, IoUtil.readBytes(file.getInputStream()));

        Map<String,Object> map = new HashMap<>();
        map.put("url", url);
        map.put("fileName", file.getName());
        map.put("newFileName", FileUtil.getName(url));
        map.put("originalFilename", file.getOriginalFilename());

        return success(map);
    }

    /**
     * 查询【请填写功能名称】列表
     */

    @GetMapping("/system/appSwiper/list")
    public CommonResult list(AppSwiper appSwiper,
                             @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                             @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<AppSwiper> page = new Page<>(pageNum, pageSize);
        IPage<AppSwiper> list = appSwiperService.selectAppSwiperList(page,appSwiper);
        return success(list);
    }

    /**
     * 导出【请填写功能名称】列表
     */

    @PostMapping("/system/appSwiper/export")
    public void export(HttpServletResponse response, AppSwiper appSwiper,
                       @RequestParam(value = "pageNum" ,required = false ,defaultValue = "1") Integer pageNum,
                       @RequestParam(value ="pageSize" ,required = false ,defaultValue = "10") Integer pageSize)
    {
        Page<AppSwiper> page = new Page<>(pageNum, pageSize);
        IPage<AppSwiper> list = appSwiperService.selectAppSwiperList(page,appSwiper);
        ExcelUtil<AppSwiper> util = new ExcelUtil<AppSwiper>(AppSwiper.class);
        util.exportExcel(response, list.getRecords(), "【请填写功能名称】数据");
    }

    /**
     * 获取【请填写功能名称】详细信息
     */

    @GetMapping(value = "/system/appSwiper/{swiperId}")
    public CommonResult getInfo(@PathVariable("swiperId") Long swiperId)
    {
        return success(appSwiperService.selectAppSwiperBySwiperId(swiperId));
    }

    /**
     * 新增【请填写功能名称】
     */

    @PostMapping("/system/appSwiper/add")
    public CommonResult add(@RequestBody AppSwiper appSwiper)
    {
        return toAjax(appSwiperService.insertAppSwiper(appSwiper));
    }

    /**
     * 修改【请填写功能名称】
     */
    @PostMapping("/system/appSwiper/edit")
    public CommonResult edit(@RequestBody AppSwiper appSwiper)
    {
        return toAjax(appSwiperService.updateAppSwiper(appSwiper));
    }

    /**
     * 删除【请填写功能名称】
     */
	@PostMapping("/system/appSwiper/del")
    public CommonResult remove(@RequestParam("idList") Long[] idList)
    {
        return toAjax(appSwiperService.deleteAppSwiperBySwiperIds(idList));
    }


    @GetMapping("/getJcPartnerInfo")
    public CommonResult getJcPartnerInfo()
    {
        LambdaQueryWrapper<ShopMallPartners> queryWrapper =new LambdaQueryWrapper<>();
        LoginUser user = SecurityFrameworkUtils.getLoginUser();
        queryWrapper.eq(ShopMallPartners::getUserId,user.getId()).eq(ShopMallPartners::getType,"jc").eq(ShopMallPartners::getIsBanned,"0");
        ShopMallPartners partner = partnersService.getOne(queryWrapper);
        if (ObjectUtil.isEmpty(partner)){
            return error("信息有误");
        }
        AdminUserDO sysUser = userService.getUser(user.getId());
        Map<String,Object> map = new HashMap<>();
        map.put("user", sysUser);
        return CommonResult.success(map);
    }
//    @GetMapping("/getJcWorkerInfo")
//    public CommonResult getJcWorkerInfo()
//    {
//
//        LambdaQueryWrapper<PartnerWorker> queryWrapper =new LambdaQueryWrapper<>();
//        SysUser user = SecurityUtils.getLoginUser().getUser();
//        queryWrapper.eq(PartnerWorker::getUserId,user.getUserId());
//        PartnerWorker worker = jcWorkerService.getOne(queryWrapper);
//        if (ObjectUtil.isNotEmpty(worker)){
//            LambdaQueryWrapper<ShopMallPartners> queryWrapper1 =new LambdaQueryWrapper<>();
//            queryWrapper1.eq(ShopMallPartners::getPartnerId,worker.getPartnerId()).eq(ShopMallPartners::getType,"jc").eq(ShopMallPartners::getIsBanned,"0");
//            ShopMallPartners partner = partnersService.getOne(queryWrapper1);
//            if (ObjectUtil.isEmpty(partner)){
//                return error("信息有误");
//            }
//            ajax.put("partnerInfo", partner);
//        }else {
//            return error("信息有误");
//        }
//        SysUser sysUser = userService.selectUserById(user.getUserId());
//
//        ajax.put("user", sysUser);
//
//        return ajax;
//    }
}
