package com.mortals.xhx.busiz.applets.web;


import com.alibaba.fastjson.JSONObject;
import com.mortals.framework.common.Rest;
import com.mortals.framework.exception.AppException;
import com.mortals.framework.model.Context;
import com.mortals.framework.model.OrderCol;
import com.mortals.framework.model.PageInfo;
import com.mortals.framework.model.Result;
import com.mortals.xhx.busiz.applets.req.ProductReq;
import com.mortals.xhx.busiz.applets.req.StaffReq;
import com.mortals.xhx.common.utils.BeanUtil;
import com.mortals.xhx.module.category.model.CategoryQuery;
import com.mortals.xhx.module.category.service.CategoryService;
import com.mortals.xhx.module.product.model.ProductEntity;
import com.mortals.xhx.module.product.model.ProductQuery;
import com.mortals.xhx.module.product.service.ProductService;
import com.mortals.xhx.module.staff.model.StaffEntity;
import com.mortals.xhx.module.staff.model.StaffQuery;
import com.mortals.xhx.module.staff.model.StaffRecordEntity;
import com.mortals.xhx.module.staff.service.StaffRecordService;
import com.mortals.xhx.module.staff.service.StaffService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.ObjectUtils;
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.RestController;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import static com.mortals.framework.ap.SysConstains.PAGEINFO_KEY;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED;
import static com.mortals.xhx.common.key.ErrorCode.ERROR_TOKEN_EXPIRED_CONTENT;


/**
 * @author ZYW
 * @date 2023-10-09 9:39
 */
@RestController
@Slf4j
@RequestMapping("/api/v1/staff")
public class StaffApiController extends AbstractBaseController<StaffReq>{
    @Autowired
    private StaffService staffService;
    @Autowired
    private StaffRecordService staffRecordService;

    /**
     * 员工详情
     */
    @PostMapping(value = "staffInfo")
    public Rest<Object> staffInfo(@RequestBody StaffReq staffReq){
        String busiDesc = "员工详情";
        log.info("【{}】【请求体】--> {}", busiDesc, JSONObject.toJSONString(staffReq));
        Rest<Object> rest = Rest.ok();
        Context context = this.getContext();
        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            throw new AppException(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            if(!ObjectUtils.isEmpty(staffReq.getId())){
                StaffEntity staffEntity = staffService.get(staffReq.getId());
                if(staffEntity != null){
                    rest.setData(staffEntity);
                }else {
                    rest = Rest.fail("未查到该员工");
                }

            }else {
                rest = Rest.fail("缺少id");
            }
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }

    /**
     * 名片被查看记录
     */
    @PostMapping(value = "staffRecord")
    public Rest<Object> staffRecord(@RequestBody StaffReq staffReq){
        String busiDesc = "名片被查看记录";
        log.info("【{}】【请求体】--> {}", busiDesc, JSONObject.toJSONString(staffReq));
        Rest<Object> rest = Rest.ok();
        Context context = this.getContext();
        if (ObjectUtils.isEmpty(context) || ObjectUtils.isEmpty(context.getUser())) {
            throw new AppException(ERROR_TOKEN_EXPIRED, ERROR_TOKEN_EXPIRED_CONTENT);
        }
        try {
            if(!ObjectUtils.isEmpty(staffReq.getId())){
                StaffRecordEntity staffRecordEntity = staffRecordService.get(staffReq.getId());
                if(staffRecordEntity != null){
                    rest.setData(staffRecordEntity);
                }else {
                    rest = Rest.fail("未查到记录");
                }

            }else {
                rest = Rest.fail("缺少id");
            }
            recordSysLog(request, busiDesc + " 【成功】");
        } catch (Exception e) {
            log.error(busiDesc, e);
            rest = Rest.fail(super.convertException(e));
        }
        return rest;
    }
}
