package com.jeeplus.assess.workClientInfo.controller;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.extra.spring.SpringUtil;
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.assess.workClientInfo.domain.WorkClientInfo;
import com.jeeplus.assess.workClientInfo.domain.dto.WorkClientInfosDto;
import com.jeeplus.assess.workClientInfo.service.WorkClientService;
import com.jeeplus.common.TokenProvider;
import com.jeeplus.core.query.QueryWrapperGenerator;
import com.jeeplus.sys.feign.IUserApi;
import com.jeeplus.sys.service.dto.UserDTO;
//import com.jeeplus.sys.utils.TicketQueryUtils;
//import com.jeeplus.sys.utils.UserUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

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

@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);
        if (ObjectUtil.isNotEmpty(workClientInfo)) {
            if (ObjectUtil.isNotEmpty(workClientInfo.getCreateDates())) {
                workClientInfoQueryWrapper.between("a.create_time",workClientInfo.getCreateDates()[0], workClientInfo.getCreateDates()[1]);
            }
            if (StringUtils.isNotBlank(workClientInfo.getCreateById())) {
                workClientInfoQueryWrapper.eq("a.create_by_id",workClientInfo.getCreateById());
            }
        }
        IPage<WorkClientInfo> list = workClientService.list(page,workClientInfoQueryWrapper);
        UserDTO userDTO = SpringUtil.getBean ( IUserApi.class ).getByToken(TokenProvider.getCurrentToken());
        for (WorkClientInfo info: list.getRecords()) {
            if(userDTO.getName().equals(info.getCreateById()) || userDTO.isAdmin()){
                info.setCreateFlag("1");
            }else{
                info.setCreateFlag("0");
            }
        }
        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(StringUtils.isBlank(workClientInfosDto.getWorkClientInfo().getName())){
                    return ResponseEntity.badRequest().body("保存信息失败，没有获取到用户名称");
                }
                if(!workClientService.haveWorkClient(workClientInfosDto)){
                    return ResponseEntity.badRequest().body("用户已存在，保存失败");
                }
                if(!workClientService.haveUscCode(workClientInfosDto)){
                    return ResponseEntity.badRequest().body("统一社会信用代码已存在，保存失败");
                }
                if(StringUtils.isNotBlank(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 = "haveUscCode")
    public boolean haveUscCode(@RequestParam String id,@RequestParam String uscCode) {
        WorkClientInfosDto workClientInfosDto = new WorkClientInfosDto();
        WorkClientInfo workClientInfo = new WorkClientInfo();
        workClientInfo.setId(id);
        workClientInfo.setUscCode(uscCode);
        workClientInfosDto.setWorkClientInfo(workClientInfo);
        return workClientService.haveUscCode(workClientInfosDto);
    }

    /**
     * 新建或修改  判断客户是否重复
     */
    @ApiOperation(value = "判断客户是否重复")
    @GetMapping(value = "haveWorkClient")
    public boolean haveWorkClient(@RequestParam String id,@RequestParam String name) {
        WorkClientInfosDto workClientInfosDto = new WorkClientInfosDto();
        WorkClientInfo workClientInfo = new WorkClientInfo();
        workClientInfo.setId(id);
        workClientInfo.setName(name);
        workClientInfosDto.setWorkClientInfo(workClientInfo);
        return workClientService.haveWorkClient(workClientInfosDto);
    }

    /**
     * 客户信息删除
     */
    @ApiOperation(value = "客户信息删除")
    @GetMapping(value = "remove")
    public Map<String,Object> 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  SpringUtil.getBean ( IUserApi.class ).enterpriseSearchByName(keyword);
    }

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

    /**
     * 根据名称、统一社会信用代码判断是否存在
     */
    @ApiOperation(value = "根据名称、统一社会信用代码判断是否存在")
    @GetMapping(value = "isExist")
    public Integer isExist(@RequestParam String param) {
        return workClientService.isExist(param);
    }

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

    /**
     * 客户信息查询-组件使用
     */
    @ApiOperation(value = "客户信息查询-组件使用")
    @GetMapping(value = "componentById")
    public WorkClientInfo componentById(@RequestParam String id) {
        return workClientService.componentById(id);
    }
}
