package org.dromara.work.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.validation.constraints.NotEmpty;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.dromara.common.core.domain.R;
import org.dromara.common.core.validate.AddGroup;
import org.dromara.common.core.validate.EditGroup;
import org.dromara.common.excel.utils.ExcelUtil;
import org.dromara.common.idempotent.annotation.RepeatSubmit;
import org.dromara.common.log.annotation.Log;
import org.dromara.common.log.enums.BusinessType;
import org.dromara.common.mail.utils.MailUtils;
import org.dromara.common.mybatis.config.DataSourceConfig;
import org.dromara.common.mybatis.core.page.PageQuery;
import org.dromara.common.mybatis.core.page.TableDataInfo;
import org.dromara.common.web.core.BaseController;
import org.dromara.work.domain.bo.TpFieldBo;
import org.dromara.work.domain.vo.TpFieldVo;
import org.dromara.work.service.ITpFieldService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.List;

/**
 * 字段管理
 *
 * @author Maosw
 * @date 2024-08-12
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/work/field")
public class TpFieldController extends BaseController {

    private final ITpFieldService tpFieldService;

    private final DataSourceConfig dataSourceConfig;

    /**
     * 查询字段管理列表
     */
    @SaCheckPermission("work:field:list")
    @GetMapping("/list")
    public TableDataInfo<TpFieldVo> list(TpFieldBo bo, PageQuery pageQuery) {
        return tpFieldService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出字段管理列表
     */
    @SaCheckPermission("work:field:export")
    @Log(title = "字段管理", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(TpFieldBo bo, HttpServletResponse response) {
        List<TpFieldVo> list = tpFieldService.queryList(bo);
        ExcelUtil.exportExcel(list, "字段管理", TpFieldVo.class, response);
    }

    /**
     * 获取字段管理详细信息
     *
     * @param id 主键
     */
    @SaCheckPermission("work:field:query")
    @GetMapping("/{id}")
    public R<TpFieldVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable Long id) {
        return R.ok(tpFieldService.queryById(id));
    }

    /**
     * 新增字段管理
     */
    @SaCheckPermission("work:field:add")
    @Log(title = "字段管理", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody TpFieldBo bo) {
        return toAjax(tpFieldService.insertByBo(bo));
    }

    /**
     * 修改字段管理
     */
    @SaCheckPermission("work:field:edit")
    @Log(title = "字段管理", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody TpFieldBo bo) {
        return toAjax(tpFieldService.updateByBo(bo));
    }

    /**
     * 删除字段管理
     *
     * @param ids 主键串
     */
    @SaCheckPermission("work:field:remove")
    @Log(title = "字段管理", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable Long[] ids) {
        return toAjax(tpFieldService.deleteWithValidByIds(List.of(ids), true));
    }


    @GetMapping("/dbConfig")
    public String getDbConfig() {
        String url = dataSourceConfig.getDatabaseUrl();
        String username = dataSourceConfig.getDatabaseUsername();
        String password = dataSourceConfig.getDatabasePassword();
        return "url：" + url + "，username：" + username + "，password：" + password;
    }

    /**
     * 发送邮件
     *
     * @param to      接收人
     * @param subject 标题
     * @param text    内容
     */
    @GetMapping("/sendSimpleMessage")
    public R<Void> sendSimpleMessage(String to, String subject, String text) throws UnknownHostException {
        InetAddress addr = InetAddress.getLocalHost();
        String domainName = addr.getHostName();

        MailUtils.sendText(to, subject, domainName);
        return R.ok();
    }
}
