package com.hxhq.system.controller;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.hxhq.business.domain.StorageLocation;
import com.hxhq.business.domain.Yq;
import com.hxhq.business.enums.NormalEnum;
import com.hxhq.business.form.yq.StorageLocationSearchForm;
import com.hxhq.business.form.yq.YqSearchForm;
import com.hxhq.business.service.IStorageLocationService;
import com.hxhq.business.service.IYqService;
import com.hxhq.system.api.domain.SysDept;
import com.hxhq.system.form.DictDataSaveForm;
import com.hxhq.system.service.ISysDeptService;
import com.hxhq.system.service.ISysDictDataService;
import com.hxhq.system.service.ISysDictTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.hxhq.common.core.utils.StringUtils;
import com.hxhq.common.core.utils.poi.ExcelUtil;
import com.hxhq.common.core.web.controller.BaseController;
import com.hxhq.common.core.web.domain.AjaxResult;
import com.hxhq.common.core.web.page.TableDataInfo;
import com.hxhq.common.log.annotation.Log;
import com.hxhq.common.log.enums.BusinessType;
import com.hxhq.common.security.utils.SecurityUtils;
import com.hxhq.system.api.domain.SysDictData;

/**
 * 数据字典信息
 * 
 * @author hxhq
 */
@RestController
@RequestMapping("/dict/data")
public class SysDictDataController extends BaseController
{
    @Autowired
    private ISysDictDataService dictDataService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

    @Autowired
    private IStorageLocationService storageLocationService;
    @Autowired
    private ISysDeptService sysDeptService;

    /**
     * 获取字典数据列表
     * @param dictData
     * @return
     */
    @GetMapping("/list")
    public TableDataInfo list(SysDictData dictData)
    {
        String ccwz = "business_ccwz";
        if(StringUtils.equals(dictData.getDictType(),ccwz)) {
            StorageLocationSearchForm form = new StorageLocationSearchForm();
            List<SysDept> superiorAndSubordinate = sysDeptService.getSuperiorAndSubordinate(SecurityUtils.getLoginUser().getSysUser().getDeptId());
            List<Long> deptIdList = superiorAndSubordinate.stream().map(SysDept::getDeptId).collect(Collectors.toList());
            form.setDeptIdList(deptIdList);
            form.setStatus(NormalEnum.yes.getValue());
            startPage();
            List<StorageLocation> list = storageLocationService.queryList(form);

            return getDataTable(list);
        } else {
            startPage();
            List<SysDictData> list = dictDataService.selectDictDataList(dictData);
            return getDataTable(list);
        }
    }

    @Log(title = "字典数据", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, SysDictData dictData)
    {
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        ExcelUtil<SysDictData> util = new ExcelUtil<SysDictData>(SysDictData.class);
        util.exportExcel(response, list, "字典数据");
    }

    /**
     * 查询字典数据详细
     */
    @GetMapping(value = "/{dictCode}")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return success(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (StringUtils.isNull(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return success(data);
    }

    /**
     * 新增字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@Validated @RequestBody DictDataSaveForm form)
    {
        SysDictData dict = form.getDict();
        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(form));
    }

    /**
     * 修改保存字典类型
     */
    @Log(title = "字典数据", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@Validated @RequestBody DictDataSaveForm form)
    {
        SysDictData dict = form.getDict();
        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(form));
    }

    /**
     * 删除字典类型
     */
    @Log(title = "字典类型", businessType = BusinessType.DELETE)
    @PostMapping("/delete")
    public AjaxResult remove(@RequestBody DictDataSaveForm form)
    {
        dictDataService.deleteDictDataByIds(form);
        return success();
    }
}
