package com.ruoyi.tc.controller;


import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.tc.entity.AssetMiniPrograms;
import com.ruoyi.tc.entity.po.AssetMiniProgramsCpPo;
import com.ruoyi.tc.entity.request.AssetMiniProgramsPageRequest;
import com.ruoyi.tc.service.AssetMiniProgramsCpService;
import com.ruoyi.tc.service.AssetMiniProgramsService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

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

import static com.ruoyi.common.core.domain.AjaxResult.success;

/**
 * 小程序资产表(asset_mini_programs)表控制层
 *
 * @author makejava
 * @since 2024-11-28 17:13:05
 */
@Api(tags = "小程序资产")
@RestController
@RequestMapping("/tc/miniPrograms")
public class AssetMiniProgramsController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AssetMiniProgramsService assetMiniProgramsService;

    /**
     * 分页查询
     *
     * @param req 分页对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询")
    @GetMapping
    public AjaxResult queryByPage(AssetMiniProgramsPageRequest req) {
        Page<AssetMiniPrograms> page = new Page<>();
        page.setCurrent(req.getCurrent());
        page.setSize(req.getSize());
        try {
            if (!SecurityUtils.getLoginUser().getUser().isAdmin() && !SecurityUtils.hasRole("common")) {
                req.setDwmc(SecurityUtils.getLoginUser().getUser().getNickName());
            }
        } catch (Exception e) {
            throw new ServiceException("获取用户信息异常");
        }
        return success(assetMiniProgramsService.page(page, req));
    }

    /**
     * 通过主键查询单条小程序数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @ApiOperation(value = "通过主键查询单条小程序数据")
    @GetMapping("/{id}")
    public AjaxResult queryById(@PathVariable("id") Long id) {
        return success(assetMiniProgramsService.getById(id));
    }

    /**
     * 新增小程序数据
     *
     * @param assetMiniPrograms 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增小程序数据")
    @PostMapping
    @Log(title = "新增小程序数据", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody AssetMiniPrograms assetMiniPrograms) {
        return success(assetMiniProgramsService.add(assetMiniPrograms));
    }

    /**
     * 编辑小程序数据
     *
     * @param assetMiniPrograms 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "编辑小程序数据")
    @Log(title = "编辑小程序数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetMiniPrograms assetMiniPrograms) {
        return success(assetMiniProgramsService.edit(assetMiniPrograms));
    }

    /**
     * 删除小程序数据
     *
     * @param id 主键
     * @return 删除是否成功
     */
    @ApiOperation(value = "删除小程序数据")
    @Log(title = "删除小程序数据", businessType = BusinessType.DELETE)
    @DeleteMapping("/delete/{id}")
    public AjaxResult deleteById(@PathVariable Long id) {
        assetMiniProgramsService.deleteById(id);
        return success();
    }

    /**
     * 下载资产导入模板
     */
    @ApiOperation(value = "下载App导入模板")
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<AssetMiniPrograms> util = new ExcelUtil<>(AssetMiniPrograms.class);
        util.importTemplateExcel(response, "App导入模板");
    }

    /**
     * 小程序导出
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "小程序导出")
    @Log(title = "小程序导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetMiniProgramsPageRequest unit) {
        List<AssetMiniPrograms> list = assetMiniProgramsService.getAllList(unit);
        ExcelUtil<AssetMiniPrograms> util = new ExcelUtil<>(AssetMiniPrograms.class);
        util.exportExcel(response, list, "小程序数据");
    }

    /**
     * 小程序导入
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "小程序导入")
    @Log(title = "小程序导入", businessType = BusinessType.IMPORT)
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<AssetMiniPrograms> util = new ExcelUtil<>(AssetMiniPrograms.class);
        List<AssetMiniPrograms> list = util.importExcel(file.getInputStream());
        if (list != null) {
            list.forEach(x->{
                AssetMiniPrograms one = assetMiniProgramsService.lambdaQuery().eq(AssetMiniPrograms::getSsdw, x.getSsdw())
                        .eq(AssetMiniPrograms::getXcxmc, x.getXcxmc()).one();
                if(one!=null){
                    x.setId(one.getId());
                    assetMiniProgramsService.edit(x);
                }else {
                    assetMiniProgramsService.add(x);
                }
            });
        } else {
            throw new ServiceException("小程序导入数据不能为空!");
        }
        return AjaxResult.success("导入成功");
    }
}

