package com.xueyi.system.staff.controller.api;

import com.alibaba.fastjson2.JSONObject;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.xueyi.common.core.constant.basic.SqlConstants;
import com.xueyi.common.core.web.result.R;
import com.xueyi.common.security.annotation.InnerAuth;
import com.xueyi.common.web.constant.ResponseCode;
import com.xueyi.file.api.feign.RemoteFileService;
import com.xueyi.system.api.digitalmans.domain.dto.DmVisitorsDto;
import com.xueyi.system.api.digitalmans.domain.po.DmVisitorsPo;
import com.xueyi.system.api.staff.domain.dto.DmStaffCommonDto;
import com.xueyi.system.api.staff.domain.po.DmStaffPo;
import com.xueyi.system.resource.controller.api.BaseApiController;
import com.xueyi.system.resource.domain.dto.DmResourcesDto;
import com.xueyi.system.resource.service.impl.DmResourcesServiceImpl;
import com.xueyi.system.resource.service.impl.FaceServiceImpl;
import com.xueyi.system.staff.mapper.DmStaffMapper;
import com.xueyi.system.staff.mapper.DmVisitorsMapper;
import com.xueyi.system.utils.common.ImageUtil;
import org.springframework.beans.factory.annotation.Autowired;
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.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

/**
 * 员工管理 API业务处理
 *
 * @author xueyi
 */
@RestController
@RequestMapping("/staff/inner-api")
public class DmStaffInnerApiController extends BaseApiController {

    @Autowired
    private DmStaffMapper dmStaffMapper;

    @Autowired
    private RemoteFileService fileService;

    @Autowired
    private FaceServiceImpl faceService;

    @Autowired
    private ImageUtil imageUtil;

    @Autowired
    private DmResourcesServiceImpl iDmResourcesService;

    @Autowired
    private DmVisitorsMapper dmVisitorsMapper;

    @Autowired
    private DmStaffMapper staffMapper;

    @InnerAuth
    @GetMapping(value = "selectOne/{staffId}")
    @ResponseBody
    public JSONObject fetchStaff(@PathVariable("staffId") String staffId){
        DmStaffPo dmStaff = dmStaffMapper.selectOne(Wrappers.<DmStaffPo>lambdaQuery().eq(DmStaffPo::getId, staffId).last(SqlConstants.LIMIT_ONE));
        if (dmStaff!= null) {
            return outputSuccess(dmStaff).toJSON();
        }
        return output(ResponseCode.DATA_NOT_EXISTS,"指定ID的员工").toJSON();
    }

    @InnerAuth
    @PostMapping(value = "new-staff")
    @ResponseBody
    public com.alibaba.fastjson2.JSONObject newStaff(@RequestBody DmStaffCommonDto commonDto){
        //访客
        if (commonDto.getStaffType().equals(DmStaffCommonDto.TYPE_STRANGER) || commonDto.getStaffType().equals(DmStaffCommonDto.TYPE_VIP_STRANGER)) {

            DmVisitorsPo v = new DmVisitorsPo();
            v.setName(commonDto.getStaffName());
            v.setNickname(commonDto.getStaffName());
            v.setType(DmVisitorsDto.TYPE_NORMAL_VISITOR);
            v.setGender(commonDto.getGender());
            v.setType(Long.parseLong(commonDto.getStaffType()));

            //兼容熟人介绍生人，传递访客照片
            if (StringUtils.isNotEmpty(commonDto.getStaffBase64Img())) {
                R r = iDmResourcesService.addBase64Img(commonDto.getStaffBase64Img(), "stranger-"+commonDto.getStaffName());

                if (r.getData() instanceof  DmResourcesDto) {
                    DmResourcesDto dmResourcesDto = (DmResourcesDto) r.getData();
                    v.setResourceId(dmResourcesDto.getId());
                    v.setAvatar(dmResourcesDto.getUrl());
                } else {
                    return output(ResponseCode.FILE_UPLOAD_FAIL, r.getMsg()).toJSON();
                }
            }

            dmVisitorsMapper.addOne(v);
        } else {//员工
            DmStaffPo staffPo = new DmStaffPo();
            staffPo.setNickName(commonDto.getStaffName());
            staffPo.setUserName(commonDto.getStaffName());
            staffPo.setUserType(commonDto.getStaffType());
            staffPo.setSex(commonDto.getGender()!=null ? String.valueOf(commonDto.getGender()):null);
            if (StringUtils.isNotEmpty(commonDto.getStaffBase64Img())) {
                R r = iDmResourcesService.addBase64Img(commonDto.getStaffBase64Img(), "staff-"+commonDto.getStaffName());

                if (r.getData() instanceof  DmResourcesDto) {
                    DmResourcesDto dmResourcesDto = (DmResourcesDto) r.getData();
                    staffPo.setResourceId(dmResourcesDto.getId());
                    staffPo.setAvatar(dmResourcesDto.getUrl());
                } else {
                    return output(ResponseCode.FILE_UPLOAD_FAIL, r.getMsg()).toJSON();
                }
            }
            dmStaffMapper.insert(staffPo);
        }

        return outputSuccess().toJSON();
    }

}
