package com.ruoyi.postCheck.controller;

import com.ruoyi.common.config.PostCheckedConfig;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.DeptCode;
import com.ruoyi.common.exception.base.BaseException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.postCheck.service.IPostSendMailService;
import com.ruoyi.postCheck.util.cnooc.SendMailUtils;
import com.ruoyi.system.service.ISysPostService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;


@Api(tags = "900.3.1-发送邮件")
@RestController
@RequestMapping("/postCheck/postSendMail")
public class PostSendMailController extends BaseController {
    @Autowired
    private SendMailUtils sendMailUtils;
    @Autowired
    private PostCheckedConfig config;

    @Autowired
    private IPostSendMailService postSendMailService;


    @Autowired
    private ISysPostService sysPostService;


    /**
     * @param serviceType 业务类型
     *                    list：岗位责任制清
     *                    programme：岗检方案
     *                    plan：岗检计划
     *                    checked：岗检表
     *                    problem：岗检问题
     *                    assessment_rating_other_1：能部门互评
     *                    assessment_rating_other_2：生产部门日常评分
     * @param serviceId   业务单据ID
     */
    @ApiImplicitParams({
            @ApiImplicitParam(name = "serviceType", value = "业务类型（list：岗位责任制清\n" +
                    "programme：岗检方案\n" +
                    "plan：岗检计划\n" +
                    "checked：岗检表\n" +
                    "problem：岗检问题\n" +
                    "assessment_rating_other_1：能部门互评\n" +
                    "assessment_rating_other_2：生产部门日常评分）", dataType = "String", dataTypeClass = String.class),
            @ApiImplicitParam(name = "serviceId", value = "业务单据ID", dataType = "Long", dataTypeClass = Long.class),
            @ApiImplicitParam(name = "content", value = "邮件内容（可以空）", dataType = "Long", dataTypeClass = Long.class),
    })
    @ApiOperation("1-发送邮件-带附件")
    @GetMapping(value = "/sendMailFile")
    public AjaxResult sendMailFile(String serviceType, Long serviceId, String content) {
        if (StringUtils.isBlank(serviceType)) {
            return error("业务类型参数为空【serviceType】，请检查！");
        }
        if (serviceId == null) {
            return error("业务单据ID为空【serviceId】，请检查！");
        }

        //list：岗位责任制清
        if (serviceType.startsWith("list_")) {
            AjaxResult result = postSendMailService.postListSendMail(serviceType, serviceId);
//            System.out.println("岗位责任清单邮件通知结果 = " + result);
            return result;
        }
        //programme：岗检方案
        else if (serviceType.startsWith("programme_")) {
            AjaxResult result = postSendMailService.postProgrammeSendMail(serviceType, serviceId);
//            System.out.println("岗检方案邮件通知结果 = " + result);
            return result;
        }
        //plan：岗检计划
        else if (serviceType.startsWith("plan_")) {
            //给检查人员发送邮件通知
            //1、生成岗检计划Excel
            //2、生成受检岗位检查表Excel
            //3、编辑邮件通知内容
            //4、调用邮件接口，发送邮件
            AjaxResult result = postSendMailService.postPalnSendMail(serviceType, serviceId);
//            System.out.println("岗检计划邮件通知结果 = " + result);
            return result;

        }
        //checked：岗检表
        else if (serviceType.startsWith("checked_")) {
            AjaxResult result = postSendMailService.postCheckedSendMail(serviceType, serviceId);
//            System.out.println("岗检表邮件通知结果 = " + result);
            return result;
        }
        //problem：岗检问题
        else if (serviceType.startsWith("problem_")) {
            AjaxResult result = postSendMailService.postProblemSendMail(serviceType, serviceId);
//            System.out.println("岗检表邮件通知结果 = " + result);
            return result;
        }
        //assessment_rating_other_10：能部门互评
        //assessment_rating_other_20：生产部门日常评分）
        else if (serviceType.startsWith("assessment_rating_other_")) {
            List<Long> userHaveAuthorityList = sysPostService.userHaveAuthorityList(DeptCode.DEPT_ICFA.getCode());
            if (!userHaveAuthorityList.contains(getUserId())) {
                return error("无权限，请切换内控法务部经理账号操作！");
            }
            AjaxResult result = postSendMailService.postAssessmentRatingOtherSendMail(serviceType, serviceId);
//            System.out.println("岗检表邮件通知结果 = " + result);
            return result;
        } else {
            return error("业务类型错误【serviceType = " + serviceType + "】，请检查！");
        }

    /*


//        刘用鹏   ex_liuyp5@cnooc.com.cn
//        方联殷   fangly@cnooc.com.cn
//        张在奎   zhangzk@cnooc.com.cn


//        String lypAddress = "刘用鹏/外协单位/惠州石化/炼化公司 <ex_liuyp5@cnooc.com.cn>"; 收件带姓名和部门信息，最优
        String lypAddress = "ex_liuyp5@cnooc.com.cn";
        String flyAddress = "fangly@cnooc.com.cn";
        String zzkAddress = "zhangzk@cnooc.com.cn";
        String address = lypAddress + ";" + flyAddress + ";" + zzkAddress;


        content = "您好：<br/>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;这是一封测试邮件，请忽略！！！<br>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;带有4个附件<br>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;收件人如下：<br>" + address;
        String[] filePaths = new String[]{
                "C:\\test\\postcheck\\1-2023.8.29 关于开展2023年度岗位责任制检查公司督导检查的通知.pdf",
                "C:\\test\\postcheck\\2-岗位责任清单-行政岗位经理.xlsx",
                "C:\\test\\postcheck\\3-岗位责任清单-后勤岗位经理.xlsx",
                "C:\\test\\postcheck\\4-岗位责任制检查表.xlsx"};

        boolean flag = false;
        try {
            flag = sendMailUtils.sendFile(content, address, filePaths);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        if (flag)
            return success(1);
        else
            return error("未知错误！");
    * */
    }

    /*
    @ApiOperation("1-发送邮件-纯文本")
    @GetMapping(value = "/sendMailText")
    public AjaxResult sendMailText() {
        String subject = "惠州石化岗检系统测试邮件";
        String content = "尊敬的领导您好：<br/>&nbsp;&nbsp;&nbsp;&nbsp;这是一封测试邮件，希望你收到以后，第一时间将此邮件转发给王凯<2174199105@qq.com>、刘用鹏<liuyongpeng@aliyun.com>，<br/>感谢您的支持与配合！";
        String to = "fangly@cnooc.com.cn";
        boolean flag = false;
        try {
            flag = sendMailUtils.sendText(subject, content, to);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        if (flag)
            return success(1);
        else
            return error("未知错误！");
    }
*/
/*
    @ApiOperation("2-发送邮件-Html")
    @GetMapping(value = "/sendMailHtml")
    public AjaxResult sendMailHtml() {
        String subject = "惠州石化岗检系统测试邮件name等于file";
        String content = "尊敬的领导您好：<br/>&nbsp;&nbsp;&nbsp;&nbsp;这是一封测试邮件，希望你收到以后，第一时间将此邮件转发给王凯<2174199105@qq.com>、刘用鹏<liuyongpeng@aliyun.com>，<br/>感谢您的支持与配合！";
        String to = "fangly@cnooc.com.cn";
//        String[] filePaths = new String[]{"C:\\test\\JAVAUploadTestFile.xlsx", "C:\\test\\JAVAUploadTestFile.docx"};
//        String[] filePaths = new String[]{"C:\\test\\javaUploadTxt1.txt", "C:\\test\\javaUploadTxt2.txt"};
//        String[] filePaths = new String[]{"C:\\test\\javaUploadExcel.xlsx", "C:\\test\\javaUploadWord.docx"};
//        JSONArray filePathsValues = new JSONArray();
//        filePathsValues.add("C:\\test\\JAVAUploadTestFile.xlsx");
//        filePathsValues.add("C:\\test\\JAVAUploadTestFile.docx");
//        System.out.println("filePathsValues = " + filePathsValues);

        if (true) {
            System.out.println("config.getSendEmailFrom() = " + config.getSendEmailFrom());
            System.out.println("config.getSendEmailHtml() = " + config.getSendEmailHtml());
            System.out.println("config.getSyncDept() = " + config.getSyncDept());
            System.out.println("config.getSyncUser() = " + config.getSyncUser());
            System.out.println("config.getDefaultUserPassword() = " + config.getDefaultUserPassword());
            return success();
        }
        boolean flag = false;
        try {
            flag = sendMailUtils.sendFile(subject, content, to, null);
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        if (flag)
            return success(1);
        else
            return error("未知错误！");
    }*/

    /**
     * @param count 1、2
     * @return
     */
    /*@ApiOperation("3-发送邮件-带附件")
    @GetMapping(value = "/sendMailFileTest")
    public AjaxResult sendMailFileTest(int count) {
//        刘用鹏   ex_liuyp5@cnooc.com.cn
//        方联殷   fangly@cnooc.com.cn
//        张在奎   zhangzk@cnooc.com.cn


//        String lypAddress = "刘用鹏/外协单位/惠州石化/炼化公司 <ex_liuyp5@cnooc.com.cn>"; 收件带姓名和部门信息，最优
        String lypAddress = "ex_liuyp5@cnooc.com.cn";
        String flyAddress = "fangly@cnooc.com.cn";
        String zzkAddress = "zhangzk@cnooc.com.cn";


//        String address = lypAddress;
        String address = lypAddress;
        if (count > 1) {
//                address = lypAddress + flyAddress;
//            } else if (count >= 3) {
            address = lypAddress + ";" + flyAddress + ";" + zzkAddress;
        }


        String subject = "中海油惠州石化岗检测试";
        String content = "您好：<br/>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;这是一封测试邮件，请忽略！！！<br>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;带有4个附件<br>" +
                "&nbsp;&nbsp;&nbsp;&nbsp;收件人如下：<br>" + address;
//        String to = "fangly@cnooc.com.cn";
//        String[] filePaths = new String[]{"C:\\test\\JAVAUploadTestFile.xlsx", "C:\\test\\JAVAUploadTestFile.docx"};
//        String[] filePaths = new String[]{"C:\\test\\javaUploadTxt1.txt", "C:\\test\\javaUploadTxt2.txt"};
        String[] filePaths = new String[]{
                "C:\\test\\postcheck\\1-2023.8.29 关于开展2023年度岗位责任制检查公司督导检查的通知.pdf",
                "C:\\test\\postcheck\\2-岗位责任清单-行政岗位经理.xlsx",
                "C:\\test\\postcheck\\3-岗位责任清单-后勤岗位经理.xlsx",
                "C:\\test\\postcheck\\4-岗位责任制检查表.xlsx"};
//        JSONArray filePathsValues = new JSONArray();
//        filePathsValues.add("C:\\test\\JAVAUploadTestFile.xlsx");
//        filePathsValues.add("C:\\test\\JAVAUploadTestFile.docx");
//        System.out.println("filePathsValues = " + filePathsValues);

        boolean flag = false;
        try {

            flag = sendMailUtils.sendFile(subject, content, address, filePaths);//刘
//            flag = sendMailUtils.sendFile(subject, content, lypAddress, filePaths);//刘
//            flag = sendMailUtils.sendFile(subject, content, lypAddress, filePaths);//刘
        } catch (Exception e) {
            e.printStackTrace();
            return error(e.getMessage());
        }
        if (flag)
            return success(1);
        else
            return error("未知错误！");
    }*/

}
