package com.hfkj.service.impl;

import com.hfkj.dao.BsDeviceOperateLogMapper;
import com.hfkj.entity.*;
import com.hfkj.service.*;
import com.hfkj.sysenum.DeviceOpTypeEnum;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Date;

@Service("deviceOperateLogService")
public class BsDeviceOperateLogServiceImpl implements BsDeviceOperateLogService {
    @Resource
    private BsDeviceOperateLogMapper deviceOperateLogMapper;
    @Resource
    private BsCompanyService companyService;
    @Resource
    private BsAgentService agentService;
    @Resource
    private BsSalesmanService salesmanService;
    @Resource
    private BsStoreService storeService;

    @Override
    public void insertLog(Long deviceId, DeviceOpTypeEnum deviceOpType, String content, Long opUserId, String opUserName,Long objectId) {
        BsDeviceOperateLog log = new BsDeviceOperateLog();
        log.setDeviceId(deviceId);
        log.setType(deviceOpType.getNumber());
        log.setContent(content);
        log.setOpUserId(opUserId);
        log.setOpUserName(opUserName);
        log.setCreateTime(new Date());


        if (deviceOpType.getNumber().equals(DeviceOpTypeEnum.type1.getNumber())
                || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type2.getNumber())
                || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type3.getNumber())) {

        } else if (deviceOpType.getNumber().equals(DeviceOpTypeEnum.type4.getNumber())
                || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type5.getNumber())) {
            BsCompany company = companyService.getCompanyById(objectId);
            log.setCompanyId(company.getId());
            log.setCompanyName(company.getName());

        } else if (deviceOpType.getNumber().equals(DeviceOpTypeEnum.type6.getNumber())
            || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type7.getNumber())) {

            BsAgent agent = agentService.getAgent(objectId);
            log.setCompanyId(agent.getCompanyId());
            log.setCompanyName(agent.getCompanyName());
            log.setAgentId(agent.getId());
            log.setAgentName(agent.getName());

        } else if (deviceOpType.getNumber().equals(DeviceOpTypeEnum.type8.getNumber())
                || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type9.getNumber())) {

            BsSalesman salesman = salesmanService.getSalesman(objectId);
            log.setCompanyId(salesman.getCompanyId());
            log.setCompanyName(salesman.getCompanyName());
            log.setAgentId(salesman.getAgentId());
            log.setAgentName(salesman.getAgentName());
            log.setSalesmanId(salesman.getId());
            log.setSalesmanName(salesman.getName());

        } else if (deviceOpType.getNumber().equals(DeviceOpTypeEnum.type10.getNumber())
                || deviceOpType.getNumber().equals(DeviceOpTypeEnum.type11.getNumber())) {

            BsStore store = storeService.getStoreById(objectId);
            log.setCompanyId(store.getCompanyId());
            log.setCompanyName(store.getCompanyName());
            log.setAgentId(store.getAgentId());
            log.setAgentName(store.getAgentName());
            log.setSalesmanId(store.getSalesmanId());
            log.setSalesmanName(store.getSalesmanName());
            log.setMerId(store.getMerId());
            log.setMerName(store.getMerName());
            log.setStoreId(store.getId());
            log.setStoreName(store.getName());
        }
        deviceOperateLogMapper.insert(log);
    }

}
