package com.bootdo.common.controller;

import com.bootdo.common.annotation.Log;
import com.bootdo.common.domain.DictDO;
import com.bootdo.common.service.DictService;
import com.bootdo.common.utils.PageUtils;
import com.bootdo.common.utils.Query;
import com.bootdo.common.utils.R;
import com.bootdo.common.utils.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 字典表
 * @author chglee
 * @email 1992lcg@163.com
 * @date 2017-09-29 18:28:07
 */

@Controller
@RequestMapping("/common/dict")
public class DictController extends BaseController {
	@Autowired
	private DictService dictService;

	@Value("${sysProvince:#{null}}")
	public String sysProvince;

	@Value("${sysCity:#{null}}")
	public String sysCity;

	@Log("访问数据字典")
	@GetMapping()
	@RequiresPermissions("common:dict:dict")
	String dict() {
		return "common/dict/dict";
	}

	@ResponseBody
	@GetMapping("/list")
	@RequiresPermissions("common:dict:dict")
	public PageUtils list(@RequestParam Map<String, Object> params) {
		// 查询列表数据
		Query query = new Query(params);
		List<DictDO> dictList = dictService.list(query);
		int total = dictService.count(query);
		PageUtils pageUtils = new PageUtils(dictList, total);
		return pageUtils;
	}

	@GetMapping("/add")
	@RequiresPermissions("common:dict:add")
	String add() {
		return "common/dict/add";
	}

	@GetMapping("/edit/{id}")
	@RequiresPermissions("common:dict:edit")
	String edit(@PathVariable("id") Long id, Model model) {
		DictDO dict = dictService.get(id);
		model.addAttribute("dict", dict);
		return "common/dict/edit";
	}

	/**
	 * 保存
	 */
	@ResponseBody
	@PostMapping("/save")
	@RequiresPermissions("common:dict:add")
	public R save(DictDO dict) {
		if (dictService.save(dict) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 修改
	 */
	@ResponseBody
	@RequestMapping("/update")
	@RequiresPermissions("common:dict:edit")
	public R update(DictDO dict) {
		dictService.update(dict);
		return R.ok();
	}

	/**
	 * 删除
	 */
	@PostMapping("/remove")
	@ResponseBody
	@RequiresPermissions("common:dict:remove")
	public R remove(Long id) {
		if (dictService.remove(id) > 0) {
			return R.ok();
		}
		return R.error();
	}

	/**
	 * 删除
	 */
	@PostMapping("/batchRemove")
	@ResponseBody
	@RequiresPermissions("common:dict:batchRemove")
	public R remove(@RequestParam("ids[]") Long[] ids) {
		dictService.batchRemove(ids);
		return R.ok();
	}

	@GetMapping("/type")
	@ResponseBody
	public List<DictDO> listType() {
		return dictService.listType();
	};

	// 类别已经指定增加
	@GetMapping("/add/{type}/{description}")
	@RequiresPermissions("common:dict:add")
	String addD(Model model, @PathVariable("type") String type, @PathVariable("description") String description) {
		model.addAttribute("type", type);
		model.addAttribute("description", description);
		return "common/dict/add";
	}

	@ResponseBody
	@GetMapping("/list/{type}")
	public List<DictDO> listByType(@PathVariable("type") String type) {
		// 查询列表数据
		Map<String, Object> map = new HashMap<>(16);
		map.put("type", type);
		List<DictDO> dictList = dictService.list(map);
		return dictList;
	}

	@ResponseBody
	@GetMapping("/list/{type}/{parentId}")
	public List<DictDO> listByTypeParentID(@PathVariable("type") String type,@PathVariable("parentId") String parentId) {
		// 查询列表数据
		Map<String, Object> map = new HashMap<>(16);
		map.put("type", type);
		map.put("parentId", parentId);
		List<DictDO> dictList = dictService.list(map);
		return dictList;
	}

	@ResponseBody
	@GetMapping("/listCity")
	public List<DictDO> listCity() {
		List<DictDO> dictList = new ArrayList<>();
		String province = getUser().getProvince();
		String city = getUser().getCity();

		// 查询列表数据
		Map<String, Object> map = new HashMap<>(16);
		if (StringUtils.isNotEmpty(city)) {
			DictDO dictDO = new DictDO();
			dictDO.setName(city);
			dictDO.setValue(city);
			dictList.add(dictDO);
			return dictList;
		}
		if (StringUtils.isNotEmpty(sysCity)) {
			DictDO dictDO = new DictDO();
			dictDO.setName(sysCity);
			dictDO.setValue(sysCity);
			dictList.add(dictDO);
			return dictList;
		}
		if(StringUtils.isNotEmpty(province)){
			map.put("description", province);//描述
			dictList = dictService.list(map);
			return dictList;
		}
		if(StringUtils.isNotEmpty(sysProvince)){
			map.put("description", sysProvince);//描述
			dictList = dictService.list(map);
			return dictList;
		}
		return dictList;
	}

	/**
	 * 所属行业
	 *
	 * @return
	 */
	@ResponseBody
	@GetMapping("/industry_type")
	public List<DictDO> industryType() {
		// 查询列表数据
		Map<String, Object> map = new HashMap<>(16);
		//描述
		map.put("description", "所属行业");
		List<DictDO> dictList = dictService.list(map);
		return dictList;
	}

	/**
	 * 获取系统省份
	 *
	 * @return
	 */
	@ResponseBody
	@GetMapping("/getSysProvince")
	public String getSysProvince() {
		return sysProvince;
	}

}
