package com.ruoyi.frequency.orderoperatorhistory.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.enums.user.UserEnums;
import com.ruoyi.frequency.customer.entity.Customer;
import com.ruoyi.frequency.customer.service.CustomerService;
import com.ruoyi.frequency.orderoperatorhistory.entity.OrderOperatorHistory;
import com.ruoyi.frequency.orderoperatorhistory.mapper.OrderOperatorHistoryMapper;
import com.ruoyi.frequency.orderoperatorhistory.service.OrderOperatorHistoryService;
import com.ruoyi.frequency.sale.entity.Sale;
import com.ruoyi.frequency.sale.service.SaleService;
import com.ruoyi.frequency.store.entity.Store;
import com.ruoyi.frequency.store.service.StoreService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 订单操作记录Service业务层处理
 *
 * @author liwenlong
 * @date 2024-06-12
 */
@Service
public class OrderOperatorHistoryServiceImpl extends ServiceImpl<OrderOperatorHistoryMapper, OrderOperatorHistory> implements OrderOperatorHistoryService {
    @Autowired
    private OrderOperatorHistoryMapper orderOperatorHistoryMapper;

    /**
     * 查询订单操作记录
     *
     * @param id 订单操作记录主键
     * @return 订单操作记录
     */
    @Override
    public OrderOperatorHistory selectOrderOperatorHistoryById(Long id) {
        return orderOperatorHistoryMapper.selectOrderOperatorHistoryById(id);
    }

    @Autowired
    private SaleService saleService;
    @Autowired
    private StoreService storeService;
    @Autowired
    private CustomerService customerService;
    @Autowired
    private ISysUserService iSysUserService;

    /**
     * 查询订单操作记录列表
     *
     * @param orderOperatorHistory 订单操作记录
     * @return 订单操作记录
     */
    @Override
    public List<OrderOperatorHistory> selectOrderOperatorHistoryList(OrderOperatorHistory orderOperatorHistory) {
        List<OrderOperatorHistory> orderOperatorHistories = orderOperatorHistoryMapper.selectOrderOperatorHistoryList(orderOperatorHistory);
        orderOperatorHistories.stream().forEach(x -> {
            if (x.getPorts() == 1) {
                if (ObjectUtil.equal(x.getUserType(), UserEnums.customer.getCode())) {
                    Customer customer = customerService.getById(x.getUserId());
                    x.setOperatorUser("编号:"+customer.getNickname() + "; 手机号:" + customer.getMobile() + "; 昵称" + customer.getName());
                } else if (ObjectUtil.equal(x.getUserType(), UserEnums.store.getCode())) {
                    Store store = storeService.getById(x.getUserId());
                    x.setOperatorUser("编号:"+store.getNickname() + "; 手机号:" + store.getMobile() + "; 昵称" + store.getName());
                } else if (ObjectUtil.equal(x.getUserType(), UserEnums.sale.getCode())) {
                    Sale sale = saleService.getById(x.getUserId());
                    x.setOperatorUser("编号:"+sale.getNickname() + "; 手机号:" + sale.getMobile() + "; 姓名:" + sale.getName());
                }
            } else {
                SysUser sysUser = iSysUserService.selectUserById(Long.valueOf(x.getCreateBy()));
                if (sysUser != null) {
                    x.setOperatorUser("账号:"+sysUser.getUserName() + "; 昵称:" + sysUser.getNickName());
                }
            }
        });
        return orderOperatorHistories;
    }


    /**
     * 新增订单操作记录
     *
     * @param orderOperatorHistory 订单操作记录
     * @return 结果
     */
    @Override
    public int insertOrderOperatorHistory(OrderOperatorHistory orderOperatorHistory) {
        orderOperatorHistory.setCreateData();
//        StringBuilder content = new StringBuilder();
//        if (orderOperatorHistory.getUserId() == null || orderOperatorHistory.getUserType() == null) {
//            content.append("后台用户【账号：" + SecurityUtils.getLoginUser().getUser().getUserName() + "；昵称：" + SecurityUtils.getLoginUser().getUser().getNickName() + "】");
//        } else {
//            if (orderOperatorHistory.getUserType().equals(UserEnums.customer.getCode())) {
//                Customer customer = customerService.getById(orderOperatorHistory.getUserId());
//                content.append("设计师【账号：" + customer.getMobile() + "；编号：" + customer.getNickname() + "；昵称：" + customer.getName() + "】");
//            } else if (orderOperatorHistory.getUserType().equals(UserEnums.store.getCode())) {
//                Store store = storeService.getById(orderOperatorHistory.getUserId());
//                content.append("表现师【账号：" + store.getMobile() + "；编号：" + store.getNickname() + "；昵称：" + store.getName() + "】");
//            } else if (orderOperatorHistory.getUserType().equals(UserEnums.sale.getCode())) {
//                Sale sale = saleService.getById(orderOperatorHistory.getUserId());
//                content.append("推广员【账号：" + sale.getMobile() + "；编号：" + sale.getNickname() + "；姓名：" + sale.getName() + "】");
//            }
//        }
//        content.append(orderOperatorHistory.getContent());
//        orderOperatorHistory.setContent(content.toString());
        return orderOperatorHistoryMapper.insert(orderOperatorHistory);
    }

    /**
     * 修改订单操作记录
     *
     * @param orderOperatorHistory 订单操作记录
     * @return 结果
     */
    @Override
    public int updateOrderOperatorHistory(OrderOperatorHistory orderOperatorHistory) {
        orderOperatorHistory.setUpdateData();
        return orderOperatorHistoryMapper.updateOrderOperatorHistory(orderOperatorHistory);
    }

    /**
     * 批量删除订单操作记录
     *
     * @param ids 需要删除的订单操作记录主键
     * @return 结果
     */
    @Override
    public int deleteOrderOperatorHistoryByIds(Long[] ids, Integer delFlag) {
        this.lambdaUpdate().set(OrderOperatorHistory::getDelFlag, delFlag).in(OrderOperatorHistory::getId, ids).update();
        return ids.length;
    }

}
