package com.zjrs.zwnw.portal.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.mohrss.leaf.core.framework.web.controller.AjaxResponse;
import org.mohrss.leaf.core.framework.web.exception.AppException;
import com.zjrs.zwnw.portal.bpo.CommonBPO;
import com.zjrs.zwnw.portal.dto.CommonDTO;
import com.zjrs.zwnw.portal.dto.LdUserDTO;
import com.zjrs.zwnw.portal.dto.common.CommonDictDTO;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import jakarta.annotation.Resource;
import jakarta.validation.Valid;
import java.util.List;
import java.util.Map;

@Api("管理")
@RestController
@RequestMapping("/api/portal/common")
public class CommonController {
    private static final Logger LOG = LoggerFactory.getLogger(CommonController.class);

    @Resource
    private CommonBPO commonBPO;

    @ApiOperation(value = "获取字典项信息", notes = "根据条件查询消息列表1")
    @PostMapping(value = "/getDicts")
    public AjaxResponse getDicts(@RequestBody CommonDictDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, Object> result = commonBPO.getDicts(dto);
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取个人待办，消息，日程个数", notes = "根据条件查询消息列表1")
    @GetMapping(value = "/getNums")
    public AjaxResponse getNums() {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            Map<String, Object> result = commonBPO.getNums();
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "获取z组织机构树父节点", notes = "根据条件查询消息列表1")
    @PostMapping(value = "/getOrgTree")
    public AjaxResponse getOrgTree(@RequestBody CommonDTO dto) {
        AjaxResponse ajaxResponse = new AjaxResponse();
        try {
            List<Object> result = commonBPO.getOrgTree(dto);
            ajaxResponse.setData("data", result);
        } catch (AppException e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg(e.getMessage());
        } catch (Exception e) {
            ajaxResponse.setAppcode("1");
            LOG.error(e.getMessage());
            ajaxResponse.setErrorMsg("系统异常");
        }
        return ajaxResponse;
    }

    @ApiOperation(value = "校验劳动系统账号密码", notes = "校验劳动系统账号密码")
    @PostMapping(value = "/checkLDUser")
    public AjaxResponse checkLDUser() {
        LOG.info("校验劳动系统账号密码开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参

        Map<String,String> resultMap = this.commonBPO.checkLDUser();
        ajaxResponse.setAppcode("0");
        ajaxResponse.setData("data", resultMap);

        LOG.info("校验劳动系统账号密码结束");
        return ajaxResponse;
    }

    @ApiOperation(value = "绑定劳动系统账号密码", notes = "绑定劳动系统账号密码")
    @PostMapping(value = "/bindLDUser")
    public AjaxResponse bindLDUser(@RequestBody @Valid LdUserDTO dto, BindingResult bindingResult) {
        LOG.info("绑定劳动系统账号密码开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参
        if(bindingResult.hasErrors()) {
            ajaxResponse.setAppcode("1");
            ajaxResponse.setErrorMsg(bindingResult.getFieldError().getDefaultMessage());
            return ajaxResponse;
        }

        Map<String,String> resultMap = this.commonBPO.bindLDUser(dto);
        ajaxResponse.setAppcode("0");
        ajaxResponse.setData("data", resultMap);

        LOG.info("绑定劳动系统账号密码结束");
        return ajaxResponse;
    }
    @ApiOperation(value = "解除绑定劳动系统账号", notes = "解除绑定劳动系统账号")
    @PostMapping(value = "/unbindLDUser")
    public AjaxResponse unbindLDUser() {
        LOG.info("解除绑定劳动系统账号开始");
        AjaxResponse ajaxResponse = new AjaxResponse();        //校验入参

        this.commonBPO.unbindLDUser();
        ajaxResponse.setAppcode("0");

        LOG.info("解除绑定劳动系统账号结束");
        return ajaxResponse;
    }
}
