package com.ruoyi.tc.controller;


import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ruoyi.common.annotation.Log;
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.AssetOfficialAccount;
import com.ruoyi.tc.entity.request.AssetOfficialAccountPageRequest;
import com.ruoyi.tc.service.AssetOfficialAccountService;
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.security.Security;
import java.util.List;

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

/**
 * 公众号资产(asset_official_account)表控制层
 *
 * @author makejava
 * @since 2024-11-28 17:13:05
 */
@Api(tags = "公众号资产")
@RestController
@RequestMapping("/tc/assetOfficialAccount")
public class AssetOfficialAccountController {
    /**
     * 服务对象
     */
    @Resource
    private AssetOfficialAccountService assetOfficialAccountService;

    /**
     * 分页查询
     *
     * @param req 分页对象
     * @return 查询结果
     */
    @ApiOperation(value = "分页查询")
    @GetMapping
    public AjaxResult queryByPage(AssetOfficialAccountPageRequest req) {
        Page<AssetOfficialAccount> 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(assetOfficialAccountService.page(page, req));
    }

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

    /**
     * 新增公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增公众号数据")
    @PostMapping
    @Log(title = "新增公众号数据", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody AssetOfficialAccount assetOfficialAccount) {
        return success(assetOfficialAccountService.add(assetOfficialAccount));
    }

    /**
     * 编辑公众号数据
     *
     * @param assetOfficialAccount 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "编辑公众号数据")
    @Log(title = "编辑公众号数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetOfficialAccount assetOfficialAccount) {
        return success(assetOfficialAccountService.edit(assetOfficialAccount));
    }

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

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

    /**
     * app导出
     */
    @Transactional(rollbackFor = Exception.class)
    @ApiOperation(value = "公众号导出")
    @Log(title = "公众号导出", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, AssetOfficialAccountPageRequest unit) {
        List<AssetOfficialAccount> list = assetOfficialAccountService.getAllList(unit);
        ExcelUtil<AssetOfficialAccount> util = new ExcelUtil<>(AssetOfficialAccount.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<AssetOfficialAccount> util = new ExcelUtil<>(AssetOfficialAccount.class);
        List<AssetOfficialAccount> list = util.importExcel(file.getInputStream());
        if (list != null) {
            list.forEach(x->{
                AssetOfficialAccount one = assetOfficialAccountService.lambdaQuery().eq(AssetOfficialAccount::getSsdw, x.getSsdw())
                        .eq(AssetOfficialAccount::getGzhmc, x.getGzhmc()).one();
                if(one!=null){
                    x.setId(one.getId());
                    assetOfficialAccountService.edit(x);
                }else {
                    assetOfficialAccountService.add(x);
                }
            });
        } else {
            throw new ServiceException("公众号导入数据不能为空!");
        }
        return AjaxResult.success("导入成功");
    }
}

