package com.unis.crk.cplbusiness.controller;

import cn.hutool.core.util.IdUtil;
import cn.hutool.json.JSONObject;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.unis.crk.basic.entity.Attaches;
import com.unis.crk.basic.service.IAttachesService;
import com.unis.crk.cplbusiness.entity.DispatcPersonnelManagement;
import com.unis.crk.cplbusiness.entity.vo.DispatcPersonnelManagementVo;
import com.unis.crk.cplbusiness.service.IDispatcPersonnelManagementService;
import com.unis.crk.utils.ResultUtil;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;


/**
 * <p>
 * 调度管理(人员管理表) 前端控制器
 * </p>
 *
 * @author TangSong
 * @since 2022-05-23
 */
@CrossOrigin
@Api(value = "调度管理(人员管理表)",tags = {"调度管理(人员管理表)"})
@RestController
@RequestMapping("dispatcPersonnelManagement")
public class DispatcPersonnelManagementController {

    private final static Logger LOGGER = LoggerFactory.getLogger(DispatcPersonnelManagementController.class);


    @Autowired
    private IDispatcPersonnelManagementService dispatcPersonnelManagementService;
    @Autowired
    private IAttachesService attachesService;

    /**
     * 根据ID查询数据
     * @param id
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getOne/{id}")
    public ResultUtil getOne(@PathVariable("id") String id){
        LOGGER.info("入参为:{}",id);
        ResultUtil<DispatcPersonnelManagement> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("传入的主键为空");
            return data;
        }
        DispatcPersonnelManagement byId = this.dispatcPersonnelManagementService.getById(id);
        Attaches attaches = attachesService.getOneAttachesByReferencesID(id);
            byId.setAttaches(attaches);
        data.success(byId);
        return data;
    }

    /**
         * 分页高级查询
         * @return
         */
    @ApiOperation(value="查询", notes="分页高级查询")
    @PostMapping(value="/findPage")
    public ResultUtil findPage(@RequestBody DispatcPersonnelManagementVo pageQuery) {
        ResultUtil<IPage<Map<String,Object>>> data = new ResultUtil<>();
        data.success(this.dispatcPersonnelManagementService.findPageVo(pageQuery));
        return data;
    }

    /**
     * 新增一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="新增", notes="新增一条数据")
    @PostMapping("add")
    public ResultUtil add(@RequestBody DispatcPersonnelManagement param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
            data.failed("所接收对象为null");
            return data;
        }
        String s = IdUtil.simpleUUID();
        param.setDataId(s);
        if(!ObjectUtils.isEmpty(param.getAttaches())){
            Attaches attaches = param.getAttaches();
            attaches.setReferencesID(s);
            attachesService.save(attaches);
        }
        data.success( this.dispatcPersonnelManagementService.save(param));
        return  data;
    }


    /**
     * 修改一条数据
     * @param param
     * @return
     */
    @ApiOperation(value="修改", notes="修改一条数据")
    @PatchMapping("edit")
    public ResultUtil edit(@RequestBody  DispatcPersonnelManagement param){
        LOGGER.info("入参为:{}", param);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (ObjectUtils.isEmpty(param)){
        data.failed("所接收对象为null");
        return data;
        }
        JSONObject ret  = this.dispatcPersonnelManagementService.update(param);
        if(!ObjectUtils.isEmpty(param.getAttaches())){
            attachesService.saveOrUpdate(param.getAttaches());
        }
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }

        return data;
    }


    /**
     * 根据ID删除数据
     * @param id
     * @return
     */
    @ApiOperation(value="删除", notes="根据ID删除数据")
    @DeleteMapping("delete/{id}")
    public ResultUtil delete(@PathVariable("id") String id){
        LOGGER.info("入参为:{}", id);
        ResultUtil<Boolean> data = new ResultUtil<>();
        if (StringUtils.isEmpty(id)){
            data.failed("所接收id为null");
            return data;
        }
        attachesService.deletAttachesByReferencesID(id);
        JSONObject ret  = this.dispatcPersonnelManagementService.delete(id);
        if (ret.getBool("success")){
        data.success(ret.getStr("msg"));
        }else {
        data.failed(ret.getStr("msg"));
        }
        return data;
    }
    /**
     * 根据单位id获取下拉
     * @param unitid 节点id
     * @return
     */
    @RequestMapping(value="/getDrop", method=RequestMethod.GET)
    @ApiOperation(value="根据单位id获取下拉人员", notes="根据单位id获取下拉人员")
    @ApiImplicitParams({
            @ApiImplicitParam(name="unitid", value="单位id", paramType="query"),
    })

    public List<DispatcPersonnelManagement> getDrop(String  unitid) {
        List<DispatcPersonnelManagement> list  = dispatcPersonnelManagementService.queryDrop(unitid);
        return list;
    }
    /**
     * 获取人员下拉
     * @return
     */
    @ApiOperation(value="查询", notes="根据ID查询数据")
    @GetMapping("getList")
    public List<DispatcPersonnelManagement> getList(){
        List<DispatcPersonnelManagement> list = dispatcPersonnelManagementService.getList();
        return list;
    }

}

