package com.blueland.user.org;

import java.util.*;
import java.util.stream.Collectors;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.blueland.annotation.ApiVersion;
import com.blueland.annotation.Log;
import com.blueland.bo.ParameterEntity;
import com.blueland.core.util.UserUtils;
import com.blueland.enums.Version;
import com.blueland.model.TUcOrg;
import com.blueland.result.R;
import com.blueland.service.IParameterService;
import com.blueland.service.ITucOrgService;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.blueland.constant.Constant;
import com.blueland.bo.Org;

import lombok.extern.slf4j.Slf4j;

import static com.blueland.constant.CommonConstant.*;


@Slf4j
@RestController
@RequestMapping(value = "/org")
public class OrgRest {

	@Autowired
	private ITucOrgService tucOrgService;

	
	/** 
	 * 搜索对象 
	 **/
	@RequestMapping(value = "",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Page<Org>> list(Org org) {
		Page<Org> page = this.tucOrgService.findPage(org);
		return R.ok(page);
	}

	
	/** 
	 * 查看对象
	 **/
	@RequestMapping(value = "/{orgId}",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Org> view(@PathVariable String orgId)  {
		Org org  = tucOrgService.getByOgId(orgId);
		return R.ok(org);
	}


	/** 
	 * 查看对象（支持多条件查询）
	 **/
	@RequestMapping(value = "get",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Org> get(Org model)  {
		Org tucOrgEntity  = tucOrgService.get(model);
		return R.ok(tucOrgEntity);
	}
	
	
	/** 
	 * 新增对象
	 **/
	@Log(busiType = "新增组织数据", opType = SAVE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Org> save(@RequestBody Org model)  {
		List<TUcOrg> list = tucOrgService.handleOrg(model);
		if(ObjectUtil.isNotEmpty(list)) {
			tucOrgService.saveBatch(list);
		}
		return R.ok(model);
	}

	
	/** 
	 * 批量新增对象
	 **/
	@Log(busiType = "批量新增组织数据", opType = SAVE, type = "web")
	@RequestMapping(value = "/batch",method = RequestMethod.POST,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> batchSave(@RequestBody List<Org> model)  {
		for (Org org : model) {
			List<TUcOrg> list = tucOrgService.handleOrg(org);
			if(ObjectUtil.isNotEmpty(list)) {
				tucOrgService.saveBatch(list);
			}
		}
		return R.ok();
	}
	
	
	/**
	 * 更新对象
	 * @return 
	 **/
	@ApiOperation("更新组织数据")
	@ApiVersion(groups = Version.SIX)
	@Log(busiType = "更新组织数据", opType = UPDATE, type = "web")
	@RequestMapping(value = "",method = RequestMethod.PUT,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Org> update(@RequestBody Org model)  {
		TUcOrg org = BeanUtil.copyProperties(model, TUcOrg.class);
		QueryWrapper<TUcOrg> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TUcOrg:: getOrgId, model.getOrgId());
		tucOrgService.update(org, queryWrapper);
		if (Constant.ORGWORKTYPE_SELL.equals(model.getOrgWorkType())) {
			tucOrgService.syncDept2CRM(model);
		}
		return R.ok(model);
	}
	
	/**
	 * 删除对象
	 * @return 
	 **/
	@ApiOperation("删除组织数据")
	@ApiVersion(groups = Version.SIX)
	@Log(busiType = "删除组织数据", opType = DELETE, type = "web")
	@RequestMapping(value = "/{orgId}",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> delete(@PathVariable String orgId) {
		QueryWrapper<TUcOrg> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().eq(TUcOrg:: getOrgId, orgId);
		tucOrgService.remove(queryWrapper);
		return R.ok();
	}
	
	/**
	 *  批量删除对象
	 * @return 
	 **/
	@Log(busiType = "批量删除组织数据", opType = DELETE, type = "web")
	@RequestMapping(value = "deleteBatch",method = RequestMethod.DELETE,produces = "application/json;charset=UTF-8")
	@ResponseBody
	@CrossOrigin
	public R<Boolean> deleteBatch(String[] ids) {
		if (ObjectUtil.isEmpty(ids)) {
			throw new RuntimeException("id不能为空");
		}
		QueryWrapper<TUcOrg> queryWrapper = new QueryWrapper<>();
		queryWrapper.lambda().in(TUcOrg:: getOrgId, Arrays.asList(ids));
		tucOrgService.remove(queryWrapper);
		return R.ok();
	}
	
	/**
	 * 子组织列表-内部抵用
	 **/
	@RequestMapping(value = "/subOrgs", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Page<Org> getSubOrgs(Org org) {
		if (StringUtils.isEmpty(org.getOrgType())) {
			throw new RuntimeException("非法参数");
		}
		org.setSortColumns("org_sort");
		Page<Org> pageInfo = tucOrgService.getOrgSubs(org);
		return pageInfo;
	}
	
	/**
	 * 组织列表-内部调用
	 **/
	@RequestMapping(value = "listOrg", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public Page<Org> listOrg(Org org) {
		if (StringUtils.isEmpty(org.getOrgType())) {
			throw new RuntimeException("非法参数");
		}
		return tucOrgService.getOrgPage(org);
	}

	@ApiOperation(value = "不分页获取组织列表", notes = "不分页获取组织列表")
	@PostMapping("/listorgNew")
	public R<List<Org>> selectOrgList(@RequestBody Org org) {
		List<Org> list = tucOrgService.getListNoPage(org);
		return R.ok(list);
	}
	
	/**
	 * 工业公司组织转换树
	 */
	@RequestMapping(value = "tree", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public List<Map<String, Object>> getOrgTreeGYGS(Org org) {
		org.setSortColumns("org_sort");
		org.setOrgStatus("QY");
		// 没有分页的组织列表数据
		String oldOrgId = org.getOrgId();
		org.setOrgId("");
		List<Org> orgList = tucOrgService.getListNoPage(org);
		org.setOrgId(oldOrgId);
		
		List<Map<String, Object>> resultList = new ArrayList<Map<String, Object>>();
		if (orgList.size() > 0) {
			resultList = tucOrgService.handleTreeData(org,
					orgList);
		}

		return resultList;
	}
	
	
	
	/**
	 * 功能描述:查询org_parent_Id
	 * @param
	 * @return
	 */
	@RequestMapping(value = "findOrgIdList",method = RequestMethod.GET,produces = "application/json;charset=UTF-8")
	@ResponseBody
	public List<Org> findOrgIdList(Org org) {
		List<Org> list = tucOrgService.findOrgIdList(org);
		return list;
	}

	@ApiVersion(groups = Version.SIX)
	@ApiOperation("获取顶级组织id")
	@GetMapping("/getTopOrgId")
	public R<String> getTopOrgId() {
		return R.ok(tucOrgService.getTopOrgId());
	}

	@ApiVersion(groups = Version.SIX)
	@ApiOperation("根据部门code获取顶级销售组织id")
	@GetMapping("/getSellOrgId/{deptCode}")
	public R<String> getSellOrgId(@PathVariable("deptCode") String deptCode) {
		return R.ok(tucOrgService.getSellOrgId(deptCode));
	}
}
