package com.zcloud.modules.custormerInfo.controller;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.zcloud.common.annotation.SysLog;
import com.zcloud.common.exception.ZException;
import com.zcloud.common.middleware.MiddlewareBean;
import com.zcloud.common.utils.HttpRequestUtil;
import com.zcloud.common.utils.PageUtils;
import com.zcloud.common.utils.R;
import com.zcloud.modules.custormerInfo.entity.BusCustomerInfoEntity;
import com.zcloud.modules.custormerInfo.service.BusCustomerInfoService;
import com.zcloud.modules.custormerInfo.vo.BusCustomerInfoVo;
import com.zcloud.modules.sys.controller.AbstractController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

/**
 * @Author makejava
 * @Desc 客户企业信息(BusCustomerInfo)表控制层
 * @Date 2024-03-29 15:49:48
 */
@Api(tags = "客户企业信息")
@RestController
@RequestMapping("busCustomerInfo")
public class BusCustomerInfoController extends AbstractController {

    @Autowired
    private BusCustomerInfoService busCustomerInfoService;

    @Resource
    private MiddlewareBean middlewareBean;

    @ApiOperation("客户企业信息-新增数据")
    @SysLog("客户企业信息-新增数据")
    @PostMapping("/save")
    public R insert(@RequestBody BusCustomerInfoEntity param) {
        param.setCreator(getUserId());
		param.setOperator(getUserId());
        return busCustomerInfoService.saveCustomerInfo(param);
    }

    @ApiOperation("客户企业信息-编辑数据")
    @SysLog("客户企业信息-编辑数据")
    @PostMapping("/update")
    public R update(@RequestBody BusCustomerInfoEntity param) {
        param.setOperator(getUserId());
        return busCustomerInfoService.updateCustomerInfo(param);
    }


    @ApiOperation("客户企业信息-编辑数据")
    @SysLog("客户企业信息-编辑数据")
    @PostMapping("/updateCustomerInfo")
    @Transactional
    public R updateCustomerInfo(
        @RequestPart BusCustomerInfoEntity param,
        @RequestPart(value="logoPath", required = false) MultipartFile logoPath,
        @RequestPart(value="filepathList", required = false) MultipartFile[] filepathList
    ) {
        param.setOperator(getUserId());
        busCustomerInfoService.updateById(param);
        String result = HttpRequestUtil.doPost(middlewareBean.getUrl() + "/busCustomerInfo/update", JSONObject.toJSONString(param));
        if (result == null || result.contains("failed")) {
            throw new ZException("数据同步错误");
        }
        return R.ok();
    }




    @ApiOperation("客户企业信息-删除数据")
    @SysLog("客户企业信息-删除数据")
    @PostMapping("/delete")
    public R delete(@RequestBody BusCustomerInfoEntity param) {
        busCustomerInfoService.delete(param.getCustomerinfoId(),getUserId());
        return R.ok();
    }

    @ApiOperation("客户企业信息-分页查询")
    @SysLog("客户企业信息-分页查询")
    @PostMapping("/listPage")
    public R listPage(@RequestBody Map<String, Object> params) {
        PageUtils page = busCustomerInfoService.listPage(params);

		return R.ok().put("page", page);
    }

    @ApiOperation("客户企业信息-全部")
    @SysLog("客户企业信息-全部")
    @PostMapping("/listAll")
    public R listAll(@RequestBody BusCustomerInfoEntity param) {
        List<BusCustomerInfoEntity> listdata = busCustomerInfoService.listAll(param);
        return R.ok().put("pd", listdata);
    }

    @ApiOperation("客户企业信息-根据ID查详情")
    @SysLog("客户企业信息-根据ID查详情")
    @PostMapping("/info")
    public R info(@RequestBody BusCustomerInfoEntity param) {
        BusCustomerInfoVo busCustomerInfo = busCustomerInfoService.getById(param.getCustomerinfoId());
        return R.ok().put("data", busCustomerInfo);
    }


    @ApiOperation("课程推送企业列表-分页查询")
    @SysLog("课程推送企业列表-分页查询")
    @PostMapping("/getCourseCustomerInfoListPage")
    public R getCourseCustomerInfoListPage(@RequestBody Map<String, String> params) {
        PageUtils page = busCustomerInfoService.getCourseCustomerInfoListPage(params);
        return R.ok().put("page", page);
    }

    @ApiOperation("客户流量管理-分页查询")
    @SysLog("客户流量管理-分页查询")
    @PostMapping("/trafficListPage")
    public R trafficListPage(@RequestBody Map<String, String> params) {
        PageUtils page = busCustomerInfoService.trafficListPage(params);
        return R.ok().put("page", page);
    }

    @PostMapping("/verifyParam")
    @ApiOperation("参数验证")
    public R verifyParam(@RequestBody Map<String, Object> params) {
        BusCustomerInfoEntity entity = busCustomerInfoService.queryByFlexibleParams(params.get("key").toString(),params.get("value").toString());
        if(entity != null ){
            return R.ok("已存在").put("entity",entity);
        }else{
            return R.ok();
        }
    }
}
