package com.ruoyi.tc.controller;


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.AssetEmail;
import com.ruoyi.tc.entity.request.AssetEmailPageRequest;
import com.ruoyi.tc.service.AssetEmailService;
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;

/**
 * 电子邮件资产表(asset_email)表控制层
 *
 * @author makejava
 * @since 2024-11-28 17:13:05
 */
@Api(tags = "电子邮件资产")
@RestController
@RequestMapping("/tc/assetEmail")
public class AssetEmailController extends BaseController {
    /**
     * 服务对象
     */
    @Resource
    private AssetEmailService assetEmailService;

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

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

    /**
     * 新增email数据
     *
     * @param assetEmail 实体
     * @return 新增结果
     */
    @ApiOperation(value = "新增email数据")
    @PostMapping
    @Log(title = "新增email数据", businessType = BusinessType.INSERT)
    public AjaxResult add(@RequestBody AssetEmail assetEmail) {
        return success(assetEmailService.add(assetEmail));
    }

    /**
     * 编辑email数据
     *
     * @param assetEmail 实体
     * @return 编辑结果
     */
    @ApiOperation(value = "编辑email数据")
    @Log(title = "编辑email数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody AssetEmail assetEmail) {
        return success(assetEmailService.edit(assetEmail));
    }

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

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

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

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

