package com.yihu.jw.base.endpoint.word;

import com.yihu.jw.base.service.doctor.excelImport.BaseDoctorExcelDO;
import com.yihu.jw.base.service.word.BaseSensitiveService;
import com.yihu.jw.base.service.word.BaseSensitiveExcelReader;
import com.yihu.jw.base.service.word.excelImport.BaseSensitiveExcelDO;
import com.yihu.jw.exception.business.ManageException;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.MixEnvelop;
import com.yihu.jw.restmodel.web.endpoint.EnvelopRestEndpoint;
import com.yihu.jw.rm.base.BaseRequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.util.List;
import java.util.Map;

/**
 * @author HZY
 * @vsrsion 1.0
 * Created at 2020/7/6
 */

@RestController
@RequestMapping(value = BaseRequestMapping.BaseSensitive.PREFIX)
@Api(value = "敏感词汇管理", description = "敏感词汇管理服务接口", tags = {"wlyy基础服务 - 敏感词汇管理服务接口"})
public class BaseSensitiveEndpoint extends EnvelopRestEndpoint {


    @Autowired
    private BaseSensitiveService baseSensitiveService;
    @Autowired
    private BaseSensitiveExcelReader baseSensitiveExcelReader;

    @Value("${fastDFS.fastdfs_file_url}")
    private String fastdfs_file_url;


    @PostMapping(value = BaseRequestMapping.BaseSensitive.createSensitiveWords)
    @ApiOperation(value = "创建敏感词汇")
    public MixEnvelop createSensitiveWords (
            @ApiParam(name = "words", value = "敏感词")
            @RequestParam(value = "words", required = false) String words,
            @ApiParam(name = "explanation", value = "说明")
            @RequestParam(value = "explanation", required = false) String explanation,
            @ApiParam(name = "status", value = "status")
            @RequestParam(value = "status", required = false,defaultValue = "1") Integer status) throws Exception {
        try {
            baseSensitiveService.createSensitiveWords(words,explanation,status+"",getUID(),getUNAME());
            return MixEnvelop.getSuccess("创建成功");
        } catch (Exception e) {
            return MixEnvelop.getError(e.getMessage());
        }
    }



    @PostMapping(value = BaseRequestMapping.BaseSensitive.updateSensitiveWords)
    @ApiOperation(value = "更新敏感词汇")
    public MixEnvelop updateSensitiveWords (
            @ApiParam(name = "id", value = "id")
            @RequestParam(value = "id", required = false) String id,
            @ApiParam(name = "words", value = "敏感词")
            @RequestParam(value = "words", required = false) String words,
            @ApiParam(name = "explanation", value = "说明")
            @RequestParam(value = "explanation", required = false) String explanation,
            @ApiParam(name = "status", value = "status")
            @RequestParam(value = "status", required = false,defaultValue = "1") String status) throws Exception {
        try {
            baseSensitiveService.updateSensitiveWords(id,words,explanation,status,getUID(),getUNAME());
            return MixEnvelop.getSuccess("更新成功");
        } catch (Exception e) {
            return MixEnvelop.getError(e.getMessage());
        }
    }




    @GetMapping(value = BaseRequestMapping.BaseSensitive.delSensitiveWords)
    @ApiOperation(value = "删除敏感词汇")
    public MixEnvelop delSensitiveWords (
            @ApiParam(name = "ids", value = "ids")
            @RequestParam(value = "ids", required = false) String ids) throws Exception {
        try {
            baseSensitiveService.delSensitiveWords(ids);
            return MixEnvelop.getSuccess("删除成功");
        } catch (Exception e) {
            return MixEnvelop.getError(e.getMessage());
        }
    }

    @GetMapping(value = BaseRequestMapping.BaseSensitive.updateSensitiveWordsStatus)
    @ApiOperation(value = "修改敏感词汇状态")
    public MixEnvelop updateSensitiveWordsStatus (
            @ApiParam(name = "ids", value = "ids")
            @RequestParam(value = "ids", required = false) String ids,
            @ApiParam(name = "status", value = "status")
            @RequestParam(value = "status", required = false,defaultValue = "1") Integer status) throws Exception {
        try {
            baseSensitiveService.updateSensitiveWordsStatus(ids,status+"",getUID(),getUNAME());
            return MixEnvelop.getSuccess("修改成功");
        } catch (Exception e) {
            return MixEnvelop.getError(e.getMessage());
        }
    }

    @GetMapping(value = BaseRequestMapping.BaseSensitive.findSensitiveWords)
    @ApiOperation(value = "查找敏感词")
    public MixEnvelop findSensitiveWords (
            @ApiParam(name = "page", value = "page")
            @RequestParam(value = "page", required = false,defaultValue = "1") Integer page,
            @ApiParam(name = "pageSize", value = "pageSize")
            @RequestParam(value = "pageSize", required = false,defaultValue = "15") Integer pageSize,
            @ApiParam(name = "status", value = "status")
            @RequestParam(value = "status", required = false) Integer status,
            @ApiParam(name = "words", value = "words")
            @RequestParam(value = "words", required = false) String words) throws Exception {
        try {
            return baseSensitiveService.findSensitiveWords(status+"",words,page,pageSize);

        } catch (Exception e) {
            return MixEnvelop.getError(e.getMessage());
        }
    }

//    @GetMapping(value = BaseRequestMapping.BaseSensitive.exportSensitiveWordsLog)
//    @ApiOperation(value = "导出日志")
//    public void exportSensitiveWordsLog (HttpServletResponse response) throws Exception {
//        response.setContentType("octets/stream");
//        response.setHeader("Content-Disposition", "attachment; filename="+ new String( "sensitiveLog.xls"));
//        OutputStream os = response.getOutputStream();
//            baseSensitiveService.exportSensitiveWordsLog(os);
//    }

    @PostMapping(value = BaseRequestMapping.BaseSensitive.importSensitiveWords)
    @ApiOperation(value = "导入敏感词")
    public Envelop importData(
            @ApiParam(name = "file", value = "文件", required = true)
            @RequestPart(value = "file") MultipartFile file,
            HttpServletRequest request,HttpServletResponse response){
        try {

            response.setContentType("octets/stream");
            response.setHeader("Content-Disposition", "attachment; filename="+ new String( "sensitiveLog.xls"));
            OutputStream os = response.getOutputStream();
            request.setCharacterEncoding("UTF-8");
            baseSensitiveExcelReader.read(file);
            //验证未通过（暂无验证）
            List<BaseSensitiveExcelDO> correctLs = baseSensitiveExcelReader.getCorrectLs();
            if(correctLs.size()>0){
                Map<String, Object> result = baseSensitiveService.importSensitive(correctLs,getUID(),getUNAME(),os,fastdfs_file_url);
                return success("导入成功!", result);
            }
        }catch (Exception e){
            e.printStackTrace();
            return failed("导入异常,请检查导入文件格式" + e.getMessage());
        }
        return failed("导入失败");
    }

}
