package com.hai.service.impl;

import com.hai.common.exception.ErrorCode;
import com.hai.common.exception.ErrorHelp;
import com.hai.common.exception.SysCode;
import com.hai.dao.HighTyAgentOilStationMapper;
import com.hai.entity.*;
import com.hai.model.TyAgentOilStationModel;
import com.hai.model.TyOrderModel;
import com.hai.service.*;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

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

@Service("highTyAgentOilStationService")
public class HighTyAgentOilStationServiceImpl implements HighTyAgentOilStationService {

    @Resource
    private HighTyAgentOilStationMapper tyAgentOilStationMapper;

    @Resource
    private HighTyAgentService tyAgentService;

    @Resource
    private HighTySalesmanService tySalesmanService;

    @Resource
    private HighTyAgentPriceService tyAgentPriceService;

    @Resource
    private HighMerchantStoreService merchantStoreService;

    @Resource
    private BsOrganizationService organizationService;

    @Override
    public void editData(HighTyAgentOilStation tyAgentOilStation) {
        if (tyAgentOilStation.getId() == null) {
            tyAgentOilStation.setStatus(1);
            tyAgentOilStation.setCreateTime(new Date());
            tyAgentOilStation.setUpdateTime(new Date());
            tyAgentOilStationMapper.insert(tyAgentOilStation);
        } else {
            tyAgentOilStation.setUpdateTime(new Date());
            tyAgentOilStationMapper.updateByPrimaryKey(tyAgentOilStation);
        }
    }

    @Override
    public List<TyAgentOilStationModel> getOilStationList(Map<String, Object> param) {
        return tyAgentOilStationMapper.queryOilStationList(param);
    }

    @Override
    public HighTyAgentOilStation getDetailById(Long id) {
        return tyAgentOilStationMapper.selectByPrimaryKey(id);
    }

    @Override
    public HighTyAgentOilStation getDetailByOilStationId(Long oilStationId) {
        HighTyAgentOilStationExample example = new HighTyAgentOilStationExample();
        example.createCriteria().andStatusNotEqualTo(0).andOilStationIdEqualTo(oilStationId);
        List<HighTyAgentOilStation> list = tyAgentOilStationMapper.selectByExample(example);
        if (list.size() > 0) {
            return list.get(0);
        }
        return null;
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void assignOrg(Long orgId, List<Long> storeList) {
        BsOrganization organization = organizationService.findById(orgId);
        if (organization == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的机构");
        }

        HighTyAgentOilStation agentOilStation;
        for (Long storeId : storeList) {
            // 查询门店
            HighMerchantStore store = merchantStoreService.getDetailById(storeId);
            if (store == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            if (getDetailByOilStationId(storeId) != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, store.getStoreName() + "，已分配给其他代理公司");
            }
            agentOilStation = new HighTyAgentOilStation();
            agentOilStation.setOrganizationId(organization.getId());
            agentOilStation.setOrganizationName(organization.getName());
            agentOilStation.setOilStationId(store.getId());
            agentOilStation.setOilStationName(store.getStoreName());
            editData(agentOilStation);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void unbindOrg(List<Long> oilStationList) {
        for (Long oilStationId : oilStationList) {
            HighTyAgentOilStation detail = getDetailById(oilStationId);
            if (detail == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            detail.setStatus(0);
            editData(detail);

            List<HighTyAgentPrice> priceList = tyAgentPriceService.getPriceList(2, detail.getOilStationId());
            for (HighTyAgentPrice price : priceList) {
                price.setStatus(0);
                tyAgentPriceService.editTyAgentPrice(price);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void assignAgent(Long agentId, List<Long> oilStationList) {
        HighTyAgent agent = tyAgentService.getDetailById(agentId);
        if (agent == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的代理商");
        }
        for (Long oilStationId : oilStationList) {
            // 查询门店
            HighTyAgentOilStation agentOilStation = getDetailById(oilStationId);
            if (agentOilStation == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            if (agentOilStation.getTyAgentId() != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, agentOilStation.getOilStationName() + "，已分配给其他代理商");
            }
            agentOilStation.setTyAgentId(agent.getId());
            agentOilStation.setTyAgentName(agent.getAgentName());
            editData(agentOilStation);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void unbindAgent(List<Long> oilStationList) {
        for (Long oilStationId : oilStationList) {
            HighTyAgentOilStation detail = getDetailById(oilStationId);
            if (detail == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            detail.setTyAgentId(null);
            detail.setTyAgentName(null);
            detail.setTySalesmanId(null);
            detail.setTySalesmanName(null);
            editData(detail);

            List<HighTyAgentPrice> priceList = tyAgentPriceService.getPriceList(2, detail.getOilStationId());
            for (HighTyAgentPrice price : priceList) {
                price.setStatus(0);
                tyAgentPriceService.editTyAgentPrice(price);
            }
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void assignSalesman(Long salesmanId, List<Long> oilStationList) {
        HighTySalesman salesman = tySalesmanService.getDetailById(salesmanId);
        if (salesman == null) {
            throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的业务员");
        }
        for (Long oilStationId : oilStationList) {
            // 查询门店
            HighTyAgentOilStation agentOilStation = getDetailById(oilStationId);
            if (agentOilStation == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            if (agentOilStation.getTySalesmanId() != null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, agentOilStation.getOilStationName() + "，已分配给其他业务员");
            }
            agentOilStation.setTySalesmanId(salesman.getId());
            agentOilStation.setTySalesmanName(salesman.getSalesmanName());
            editData(agentOilStation);
        }
    }

    @Override
    @Transactional(rollbackFor=Exception.class,propagation= Propagation.REQUIRES_NEW)
    public void unbindSalesman(List<Long> oilStationList) {
        for (Long oilStationId : oilStationList) {
            HighTyAgentOilStation detail = getDetailById(oilStationId);
            if (detail == null) {
                throw ErrorHelp.genException(SysCode.System, ErrorCode.COMMON_ERROR, "未知的加油站");
            }
            detail.setTySalesmanId(null);
            detail.setTySalesmanName(null);
            editData(detail);
        }
    }

    @Override
    public List<TyOrderModel> getOrderList(Map<String, Object> param) {
        return tyAgentOilStationMapper.getOrderList(param);
    }

}
