package com.yeejoin.amos.boot.module.common.biz.controller;

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.yeejoin.amos.boot.biz.common.controller.BaseController;
import com.yeejoin.amos.boot.biz.common.entity.DataDictionary;
import com.yeejoin.amos.boot.biz.common.service.impl.DataDictionaryServiceImpl;
import com.yeejoin.amos.boot.biz.common.utils.Menu;
import com.yeejoin.amos.boot.biz.common.utils.RedisUtils;
import com.yeejoin.amos.boot.biz.common.utils.TreeParser;
import com.yeejoin.amos.boot.module.common.api.dto.FireExpertsDto;
import com.yeejoin.amos.boot.module.common.api.entity.FireExperts;
import com.yeejoin.amos.boot.module.common.biz.service.impl.FireExpertsServiceImpl;
import com.yeejoin.amos.boot.module.common.biz.utils.BeanDtoVoUtils;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.typroject.tyboot.core.foundation.enumeration.UserType;
import org.typroject.tyboot.core.restful.doc.TycloudOperation;
import org.typroject.tyboot.core.restful.utils.ResponseHelper;
import org.typroject.tyboot.core.restful.utils.ResponseModel;
import java.util.*;
import java.util.stream.Collectors;


/**
 * 消防专家
 *
 * @author system_generator
 * @date 2021-06-25
 */
@RestController
@Api(tags = "消防专家Api")
@RequestMapping(value = "/common/fire-experts")
public class FireExpertsController extends BaseController {

    @Autowired
    FireExpertsServiceImpl fireExpertsServiceImpl;

    @Autowired
    RedisUtils redisUtils;

    @Autowired
    DataDictionaryServiceImpl dataDictionaryService;

    /**
     * 新增
     *
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PostMapping(value = "/save")
    @ApiOperation(httpMethod = "POST", value = "新增", notes = "新增")
    public ResponseModel<FireExpertsDto> save(@RequestBody FireExpertsDto model) {
        model = fireExpertsServiceImpl.createWithModel(model);
        return ResponseHelper.buildResponse(model);
    }

    /**
     * 根据sequenceNbr更新
     *
     * @param sequenceNbr 主键
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @PutMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "PUT", value = "根据sequenceNbr更新", notes = "根据sequenceNbr更新")
    public ResponseModel<FireExpertsDto> updateBySequenceNbrFireExperts(@RequestBody FireExpertsDto model,
                                                                        @PathVariable(value = "sequenceNbr") Long sequenceNbr) {
        model.setSequenceNbr(sequenceNbr);
        return ResponseHelper.buildResponse(fireExpertsServiceImpl.updateWithModel(model));
    }

    /**
     * 根据sequenceNbr删除
     *
     * @param sequenceNbr 主键
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @DeleteMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "DELETE", value = "根据sequenceNbr删除", notes = "根据sequenceNbr删除")
    public ResponseModel<Boolean> deleteBySequenceNbr(@PathVariable(value = "sequenceNbr") Long sequenceNbr) {
        FireExperts fireExperts = fireExpertsServiceImpl.getById(sequenceNbr);
        fireExperts.setIsDelete(true);
        return ResponseHelper.buildResponse(fireExpertsServiceImpl.updateById(fireExperts));
    }

    /**
     * 根据sequenceNbr查询
     *
     * @param sequenceNbr 主键
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/{sequenceNbr}")
    @ApiOperation(httpMethod = "GET", value = "根据sequenceNbr查询单个", notes = "根据sequenceNbr查询单个")
    public ResponseModel<FireExpertsDto> selectOne(@PathVariable Long sequenceNbr) {
        FireExpertsDto fireExpertsDto = fireExpertsServiceImpl.queryBySeq(sequenceNbr);
        Date birthdayTime = fireExpertsDto.getBirthdayTime();
        int age = BeanDtoVoUtils.getAge(birthdayTime);
        fireExpertsDto.setAge(age);
        return ResponseHelper.buildResponse(fireExpertsDto);
    }

    /**
     * 列表分页查询
     *
     * @param current        当前页
     * @param size           每页大小
     * @param fireExpertsDto 查询参数
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @GetMapping(value = "/page")
    @ApiOperation(httpMethod = "GET", value = "分页查询", notes = "分页查询")
    public ResponseModel<IPage<FireExpertsDto>> queryForPage(@RequestParam(value = "current") int current,
                                                             @RequestParam(value = "size") int size,
                                                             FireExpertsDto fireExpertsDto) {
        Page<FireExpertsDto> page = new Page<>();
        page.setCurrent(current);
        page.setSize(size);
        Page<FireExpertsDto> fireExpertsDtoPage = fireExpertsServiceImpl.queryForFireExpertsPage(page, false,
                fireExpertsDto.getName(), fireExpertsDto.getExpertCode());
        List<FireExpertsDto> fireExpertsDtoList = fireExpertsDtoPage.getRecords().stream().map(item -> {
            item.setAge(BeanDtoVoUtils.getAge(item.getBirthdayTime()));
            return item;
        }).collect(Collectors.toList());
        fireExpertsDtoPage.setRecords(fireExpertsDtoList);
        return ResponseHelper.buildResponse(fireExpertsDtoPage);
    }

    /**
     * 列表全部数据查询
     *
     * @return 返回结果
     */
    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @ApiOperation(httpMethod = "GET", value = "列表全部数据查询", notes = "列表全部数据查询")
    @GetMapping(value = "/list")
    public ResponseModel<List<FireExpertsDto>> selectForList() {
        List<FireExpertsDto> fireExpertsDtoList = fireExpertsServiceImpl.queryForFireExpertsList(false);
        fireExpertsDtoList.stream().map(item -> {
            item.setAge(BeanDtoVoUtils.getAge(item.getBirthdayTime()));
            return item;
        }).collect(Collectors.toList());

        return ResponseHelper.buildResponse(fireExpertsDtoList);
    }

    @TycloudOperation(ApiLevel = UserType.AGENCY)
    @RequestMapping(value = "/getExpertsTree", method = RequestMethod.GET)
    @ApiOperation(httpMethod = "GET", value = "获取专家领域树", notes = "获取专家领域树")
    public ResponseModel<Object> getExpertsTree() throws Exception {
        String type = "ZJLY";
        QueryWrapper<DataDictionary> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type", type);
        queryWrapper.orderByAsc("sort_num");

        Collection<DataDictionary> list = dataDictionaryService.list(queryWrapper);
        List<Menu> menus = TreeParser.getTree(null, list, DataDictionary.class.getName(), "getCode", 0, "getName"
                , "getParent",null);
        Menu menu = new Menu(-1L, "专家领域", -1L, menus,0);
        List<Menu> menuList = new ArrayList<>();
        menuList.add(menu);
        // 创建挂在主节点
        return ResponseHelper.buildResponse(menuList);
    }
}
