package com.ruixuan.isc.controller;

import com.alibaba.fastjson.JSONObject;
import com.ruixuan.common.core.controller.BaseController;
import com.ruixuan.common.core.domain.AjaxResult;
import com.ruixuan.common.core.page.TableDataInfo;
import com.ruixuan.common.utils.Check;
import com.ruixuan.common.utils.Result;
import com.ruixuan.isc.entity.JYKuaishouPromoter;
import com.ruixuan.isc.entity.KuaishouPromoter;
import com.ruixuan.isc.service.IKuaishouPromoterService;
import com.ruixuan.isc.utils.Tess4jClient;
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.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import java.util.List;

/**
 * 快手达人 信息Controller
 *
 * @author ruoyi
 * @date 2023-02-03
 */

@Api(tags = "快手达人")
@RestController
@RequestMapping("/kuaishou/promoter")
public class KuaishouPromoterController extends BaseController {
    @Autowired
    private IKuaishouPromoterService kuaishouPromoterService;

    @Value("${tessdata.path}")
    private String path;


    /**
     * 查询快手达人 信息列表
     */
    @GetMapping("/addRule")
    @ApiOperation(value = "查询快手达人")
    public JSONObject addRule(String promoterId, Long ruleId) {

        JSONObject returnJson = new JSONObject();
        try {
            if (Check.isNull(promoterId)) {
                throw new Exception("请选择需要绑定的达人");
            }
            if (Check.isNotNull(promoterId)) {
                promoterId = promoterId.trim();
            }
            if (Check.isNull(ruleId)) {
                throw new Exception("请选择需要绑定的规则");
            }

            String ruleName = kuaishouPromoterService.getRuleName(ruleId);

            int i = kuaishouPromoterService.addRule(promoterId, ruleId, ruleName);
            if (i > 0) {
                returnJson.put("code", 0);
                returnJson.put("message", "修改成功");
            }
        } catch (Exception e) {
            e.printStackTrace();
            returnJson.put("code", -1);
            returnJson.put("message", e.getMessage());
        }
        return returnJson;

    }


    /**
     * 查询快手达人 信息列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "查询快手达人")
    public TableDataInfo list(@ApiParam("创建人ID") @RequestParam(value = "userId", required = true) Long userId,
                              @ApiParam("达人ID") @RequestParam(value = "promoterId", required = false) String promoterId,
                              @ApiParam("达人名称") @RequestParam(value = "promoterNickName", required = false) String promoterNickName,
                              @ApiParam("媒体") @RequestParam(value = "mediaId", required = false) String mediaId,
                              @ApiParam("排序参数") @RequestParam(value = "parameter", required = false) String parameter,
                              @ApiParam("正序倒叙") @RequestParam(value = "orderBy", required = false) String orderBy

    ) {

        if (Check.isNotNull(promoterId)) {
            promoterId = promoterId.trim();
        }
        List<KuaishouPromoter> list = kuaishouPromoterService.selectKuaishouPromoterList(userId, promoterId, promoterNickName, mediaId, parameter, orderBy);
        return getDataTable(list);
    }

    /**
     * 达人公海
     */
    @GetMapping("/list2")
    @ApiOperation(value = "达人公海")
    public TableDataInfo list2(@ApiParam("达人ID") @RequestParam(value = "promoterId", required = false) String promoterId,
                               @ApiParam("渠道人员ID") @RequestParam(value = "bdUserId", required = false) Long bdUserId,
                               @ApiParam("达人名称") @RequestParam(value = "promoterNickName", required = false) String promoterNickName,
                               @ApiParam("认领状态，1-认领，2-未认领") @RequestParam(value = "status", required = false) String status,
                               @ApiParam("排序参数") @RequestParam(value = "parameter", required = false) String parameter,
                               @ApiParam("正序倒叙") @RequestParam(value = "orderBy", required = false) String orderBy) {
        if (Check.isNotNull(promoterId)) {
            promoterId = promoterId.trim();
        }
        List<JSONObject> list = kuaishouPromoterService.selectKuaishouPromoterList2(bdUserId, promoterId, promoterNickName, status, parameter, orderBy);
        return getDataTable(list);
    }


    /**
     * 查询自营、分销达人 信息列表
     */
    @GetMapping("/list3")
    @ApiOperation(value = "查询骄阳达人")
    public TableDataInfo list3(@ApiParam("创建人ID") @RequestParam(value = "userId", required = true) Long userId,
                               @ApiParam("达人ID") @RequestParam(value = "promoterId", required = false) String promoterId,
                               @ApiParam("达人名称") @RequestParam(value = "promoterNickName", required = false) String promoterNickName,
                               @ApiParam("媒体") @RequestParam(value = "mediaId", required = false) String mediaId,
                               @ApiParam("类型") @RequestParam(value = "type", required = false) String type,
                               @ApiParam("排序参数") @RequestParam(value = "parameter", required = false) String parameter,
                               @ApiParam("正序倒叙") @RequestParam(value = "orderBy", required = false) String orderBy

    ) {

        if (Check.isNotNull(promoterId)) {
            promoterId = promoterId.trim();
        }
        List<JYKuaishouPromoter> list = kuaishouPromoterService.selectKuaishouPromoterList3(userId, promoterId, promoterNickName, mediaId, type, parameter, orderBy);
        return getDataTable(list);
    }

    /**
     * 获取快手达人 信息详细信息
     */
    @GetMapping(value = "/getById")
    @ApiOperation(value = "获取快手达人信息详细信息")
    public AjaxResult getById(@ApiParam("主键") @RequestParam(value = "id", required = true) Long id) {
        return AjaxResult.success(kuaishouPromoterService.selectKuaishouPromoterById(id));
    }

    /**
     * 获取快手达人信息
     */
    @GetMapping(value = "/supplementInfo")
    @ApiOperation(value = "补全失败的数据")
    public Result supplementInfo() {
        return kuaishouPromoterService.supplementInfo();
    }

    /**
     * 获取快手达人信息
     */
    @GetMapping(value = "/getPromoterInfo")
    @ApiOperation(value = "获取快手达人短视频信息")
    public AjaxResult getPromoterInfo(@ApiParam("达人ID") @RequestParam(value = "promoterId", required = true) String promoterId) {

        return kuaishouPromoterService.getPromoterInfo(promoterId);
    }


    /**
     * 获取快手达人信息
     */
    @GetMapping(value = "/getOnlyPromoterInfo")
    @ApiOperation(value = "只获取达人信息")
    public AjaxResult getOnlyPromoterInfo(@ApiParam("达人ID") @RequestParam(value = "promoterId", required = true) String promoterId) {
        if (Check.isNotNull(promoterId)) {
            promoterId = promoterId.trim();
        }
        return AjaxResult.success(kuaishouPromoterService.getOnlyPromoterInfo(promoterId));
    }

    /**
     * 新增快手达人 信息
     */
    @PostMapping(value = "/add")
    @ApiOperation(value = "新增快手达人")
    public JSONObject add(@RequestBody KuaishouPromoter kuaishouPromoter) {
        try {
            return kuaishouPromoterService.insertKuaishouPromoter(kuaishouPromoter);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject result = new JSONObject();
            result.put("code", -1);
            result.put("msg", "失败");
            return result;
        }
    }

    /**
     * 新增骄阳达人 信息
     */
    @PostMapping(value = "/add3")
    @ApiOperation(value = "新增骄阳达人")
    public JSONObject add3(@RequestBody JYKuaishouPromoter kuaishouPromoter) {
        try {
            return kuaishouPromoterService.insertKuaishouPromoter3(kuaishouPromoter);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject result = new JSONObject();
            result.put("code", -1);
            result.put("msg", "失败");
            return result;
        }
    }

    /**
     * 新增 设置渠道绑定达人数
     */
    @GetMapping(value = "/addPromotersCount")
    @ApiOperation(value = "新增设置渠道绑定达人数")
    public JSONObject addPromotersCount(Long userId, Long promotersCount, String mediaId) {
        try {
            return kuaishouPromoterService.addPromotersCount(userId, promotersCount, mediaId);
        } catch (Exception e) {
            e.printStackTrace();
            JSONObject result = new JSONObject();
            result.put("code", -1);
            result.put("msg", "失败");
            return result;
        }
    }

    /**
     * 新增达人跟进记录
     */
    @PostMapping(value = "/addFollowUpRecords")
    @ApiOperation(value = "新增达人跟进记录")
    public Result addFollowUpRecords(@RequestBody JSONObject result) {
        try {
            kuaishouPromoterService.addFollowUpRecords(result);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("失败");
        }
    }

    /**
     * 新增达人跟进记录
     */
    @PostMapping(value = "/updateFollowUpRecords")
    @ApiOperation(value = "修改达人跟进记录")
    public Result updateFollowUpRecords(@RequestBody JSONObject result) {
        try {
            kuaishouPromoterService.updateFollowUpRecords(result);
            return Result.success();
        } catch (Exception e) {
            e.printStackTrace();
            return Result.error("失败");
        }
    }

    /**
     * 修改快手达人 信息
     */
    @PostMapping(value = "/edit")
    @ApiOperation(value = "修改快手达人")
    public AjaxResult edit(@RequestBody KuaishouPromoter kuaishouPromoter) {
        return toAjax(kuaishouPromoterService.updateKuaishouPromoter(kuaishouPromoter));
    }

    /**
     * 修改骄阳达人 信息
     */
    @PostMapping(value = "/edit3")
    @ApiOperation(value = "修改骄阳达人")
    public AjaxResult edit3(@RequestBody JYKuaishouPromoter kuaishouPromoter) {
        return toAjax(kuaishouPromoterService.updateKuaishouPromoter3(kuaishouPromoter));
    }

    /**
     * 修改快手达人标签信息
     */
    @PostMapping(value = "/updatePromoterLabel")
    @ApiOperation(value = "修改快手达人标签")
    public Result updatePromoterLabel(@RequestBody JSONObject result) {
        return kuaishouPromoterService.updatePromoterLabel(result);
    }

    /**
     * 删除快手达人 信息
     */
    @GetMapping("/deleteById")
    @ApiOperation(value = "删除快手达人")
    public Result deleteById(@ApiParam("主键") @RequestParam(value = "id", required = true) Long id,
                             @ApiParam("操作人ID") @RequestParam(value = "userId", required = true) Long userId) {
        return kuaishouPromoterService.deleteKuaishouPromoterById(id, userId);
    }

    /**
     * 删除骄阳达人 信息
     */
    @GetMapping("/deleteById3")
    @ApiOperation(value = "删除骄阳达人")
    public Result deleteById3(@ApiParam("主键") @RequestParam(value = "id", required = true) Long id,
                              @ApiParam("操作人ID") @RequestParam(value = "userId", required = true) Long userId) {
        return kuaishouPromoterService.deleteKuaishouPromoterById3(id, userId);
    }


    /**
     * 读取图片文字信息
     */
    @PostMapping(value = "/readImage")
    @ApiOperation(value = "读取图片文字信息")
    public JSONObject readImage(@RequestParam("file") MultipartFile file) {
        JSONObject result = new JSONObject();
        try {
            result.put("code", 0);
            result.put("message", "success");
            result.put("data", Tess4jClient.getWords(file, path));
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        result.put("code", -1);
        result.put("message", "fail");
        return result;
    }

    /**
     * 获取达人公海视频销售
     */
    @GetMapping("/getGongHaiVideoSales")
    @ApiOperation(value = "获取达人公海视频销售")
    public Result getGongHaiVideoSales(@ApiParam("达人ID") @RequestParam(value = "promoterId", required = false) String promoterId) {
        return kuaishouPromoterService.getGongHaiVideoSales(promoterId);
    }
}
