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


import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.node.ObjectNode;
import com.yihu.fastdfs.FastDFSUtil;

import com.yihu.jw.base.service.clientSearch.ClientSearchService;
import com.yihu.jw.entity.base.searchClient.BaseSearchNoveltyRepeatDO;
import com.yihu.jw.restmodel.web.Envelop;
import com.yihu.jw.restmodel.web.ObjEnvelop;
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.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.Base64;
import java.util.List;
import java.util.Map;

/**
 * Created by wsy on 2021/9/4.
 */
@RestController
@RequestMapping(value = BaseRequestMapping.ClientSearch.PREFIX)
@Api(value = "查新查重服务",description = "查新查重服务",tags = {"查新查重服务"})
public class ClientSearchEndpoint extends EnvelopRestEndpoint {

    @Autowired
    private ClientSearchService clientSearchService;


    @PostMapping(value = BaseRequestMapping.ClientSearch.CREATE)
    @ApiOperation(value = "保存查新查重服务")
    public Envelop saveBaseSearchNoveltyRepeat(@ApiParam(name = "jsonData",value = "")
                                               @RequestParam(name = "jsonData", required = true) String jsonData) throws Exception{
        try{
            BaseSearchNoveltyRepeatDO baseSearchNoveltyRepeatDO = objectMapper.readValue(jsonData, BaseSearchNoveltyRepeatDO.class);
            clientSearchService.saveBaseSearchNoveltyRepeat(baseSearchNoveltyRepeatDO);
            return success("success");
        }catch (Exception e){
            return failedObjEnvelopException(e);
        }

    }

    @GetMapping(BaseRequestMapping.ClientSearch.DELETEBYID)
    @ApiOperation(value = "删除查新查重")
    public Envelop deleteSearchNoveltyRepeatById(@ApiParam(name = "id",value = "")
                                                 @RequestParam(name = "id", required = true) String id) throws Exception{
        try{
            clientSearchService.deleteByid(id);
            return success("success");
        }catch(Exception e){
            return failedException(e);
        }
    }

    @GetMapping(BaseRequestMapping.ClientSearch.FINDBYSEARCHDATA)
    @ApiOperation(value = "根据搜索查询")
    public Envelop findSearchNoveltyRepeatById(@ApiParam(name = "SearchData", value = "查询字段")
                                               @RequestParam(name = "SearchData", required = true) String SearchData) throws Exception{
        try{
            if(SearchData == null || SearchData.trim().length() == 0){
                return failed("输入参数为空,请重新输入");
            }
            List list;
            list = clientSearchService.findBySearchData(SearchData);
            return success("success",list);
        }catch (Exception e){
            return failedException(e);
        }
    }

    @GetMapping(BaseRequestMapping.ClientSearch.FINDALL)
    @ApiOperation("查询所有")
    public Envelop findSearchNoveltyRepeatAll( @ApiParam(name = "fields", value = "返回的字段，为空返回全部字段")
                                               @RequestParam(value = "fields", required = false) String fields,
                                               @ApiParam(name = "filters", value = "过滤器，为空检索所有条件")
                                               @RequestParam(value = "filters", required = false) String filters,
                                               @ApiParam(name = "sorts", value = "排序，规则参见说明文档")
                                               @RequestParam(value = "sorts", required = false) String sorts,
                                               @ApiParam(name = "page", value = "第几页，从1开始", required = true, defaultValue = "1")
                                               @RequestParam(value = "page", required = true,defaultValue = "1")Integer page,
                                               @ApiParam(name = "pageSize", value = "每页分页大小", required = true, defaultValue = "10")
                                               @RequestParam(value = "pageSize", required = true,defaultValue = "10")Integer pageSize) throws Exception{
        try{
            List<BaseSearchNoveltyRepeatDO> list = clientSearchService.search(fields,filters,"-createTime",page,pageSize);
            int count = (int) clientSearchService.getCount(filters);
            return success("success",200,list,count,page,pageSize);
        }catch (Exception e){
            return failedException(e);
        }
    }

    @GetMapping(BaseRequestMapping.ClientSearch.CHANGESTATUS)
    @ApiOperation("变更状态")
    public Envelop changeStatus(@ApiParam(name = "id", value = "")
                                @RequestParam(value = "id",required = true) String id,
                                @ApiParam(name = "status",value = "1 待接收、2 办理中、3 已退回、4 已办结")
                                @RequestParam(name = "status",required = true) Integer status){
        try{
            clientSearchService.updateById(id,status);
            return success("success");
        }catch (Exception e){
            return failedException(e);
        }
    }

    @PostMapping(BaseRequestMapping.ClientSearch.REPLY)
    @ApiOperation("答复")
    public Envelop reply(@ApiParam(name = "id", value = "")
                         @RequestParam(value = "id",required = true) String id,
                         @ApiParam(name = "reply",value = "")
                         @RequestParam(name = "reply",required = true) String reply,
                         @ApiParam(name = "updateUser",value = "答复操作人")
                         @RequestParam(name = "updataUser",required = true) String updataUser){
        try{
            clientSearchService.updateReply(id,reply,updataUser);
            return success("success");
        }catch (Exception e){
            return failedException(e);
        }
    }

    @PostMapping(BaseRequestMapping.ClientSearch.SHOWTYPE)
    @ApiOperation("隐藏与显示")
    public Envelop ShowType(@ApiParam(name = "id",value = "")
                            @RequestParam(value = "id",required = true) String id,
                            @ApiParam(name = "showType",value = "")
                            @RequestParam(value = "showType",required = true) String showType){
        try{
            clientSearchService.updataShowType(id,showType);
            return success("success");
        }catch (Exception e){
            return failedException(e);
        }
    }

}
