package com.jeeplus.test.workClientInfo.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.utils.StringUtils;
import com.jeeplus.sys.utils.TicketQueryUtils;
import com.jeeplus.test.projectRecords.domain.Project;
import com.jeeplus.test.workClientInfo.domain.WorkClientInfo;
import com.jeeplus.test.workClientInfo.domain.dto.PageInfoDto;
import com.jeeplus.test.workClientInfo.domain.dto.WorkClientInfosDto;
import com.jeeplus.test.workClientInfo.service.WorkClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@Slf4j
@Api(tags ="workClientInfo")
@RestController
@RequestMapping(value = "/workClientInfo/workClientInfo")
public class WorkClientController {

    @Resource
    private WorkClientService workClientService;

    /**
     * 客户信息列表
     */
    @ApiOperation(value = "客户信息列表")
    @GetMapping(value = "list")
    public ResponseEntity<IPage<WorkClientInfo>> list(WorkClientInfo workClientInfo, Page<WorkClientInfo> page) throws Exception {
        QueryWrapper<WorkClientInfo> workClientInfoQueryWrapper = QueryWrapperGenerator.buildQueryCondition(workClientInfo, WorkClientInfo.class);
        IPage<WorkClientInfo> list = workClientService.list(page,workClientInfoQueryWrapper);
        return ResponseEntity.ok (list);
    }

    /**
     * 客户信息新增/修改
     */
    @ApiOperation(value = "客户信息新增/修改")
    @PostMapping(value = "save")
    public ResponseEntity save(@RequestBody WorkClientInfosDto workClientInfosDto) {
        if(ObjectUtil.isNotEmpty(workClientInfosDto)){
            if(ObjectUtil.isNotEmpty(workClientInfosDto.getWorkClientInfo())){
                if(StrUtil.isNotEmpty(workClientInfosDto.getWorkClientInfo().getId())){
                    return ResponseEntity.ok(workClientService.update(workClientInfosDto));
                }else{
                    return ResponseEntity.ok(workClientService.save(workClientInfosDto));
                }
            }else{
                return ResponseEntity.badRequest().body("保存信息失败，没有获取到数据");
            }
        }
        return ResponseEntity.badRequest().body("保存信息失败，没有获取到数据");
    }

    /**
     * 客户信息删除
     */
    @ApiOperation(value = "客户信息删除")
    @GetMapping(value = "remove")
    public String remove(@RequestParam String id) {
        return workClientService.removeById(id);
    }

    /**
     * 客户信息查询
     */
    @ApiOperation(value = "客户信息查询")
    @GetMapping(value = "findById")
    public WorkClientInfosDto findById(@RequestParam String id) {
        return workClientService.findById(id);
    }

    /**
     * 客户信息更新
     */
    @ApiOperation(value = "客户信息更新")
    @PostMapping(value = "update")
    public String update(@RequestBody WorkClientInfosDto workClientInfosDto) {
        return workClientService.update(workClientInfosDto);
    }

    /**
     * 根据名称模糊搜索客户信息
     */
    @ApiOperation(value = "根据名称模糊搜索客户信息")
    @GetMapping(value = "findListByName")
    public List<WorkClientInfo> findListByName(@RequestParam String name) {
        return workClientService.findListByName(name);
    }

    /**
     * 根据关键字查询相关企业
     * @param keyword
     * @return
     */
    @ApiOperation(value = "根据关键字查询相关企业")
    @PostMapping("enterpriseSearchByName")
    public String enterpriseSearchByName(@RequestParam String keyword){
        if(StringUtils.isBlank(keyword)){
            return null;
        }
        return  TicketQueryUtils.enterpriseSearchByName(keyword);
    }

    /**
     * 根据id查询企业税号
     * @param id
     * @return
     */
    @ApiOperation(value = "根据id查询企业税号")
    @PostMapping("enterpriseTicketInfo")
    public Object enterpriseTicketInfo(String id){
        if(StringUtils.isBlank(id)){
            return null;
        }
        return TicketQueryUtils.enterpriseTicketInfoQueryById(id);
    }
}
