package com.neusoft.smart.pos.inventory.service;

import com.neusoft.smart.pos.framework.exception.BusinessCommonException;
import com.neusoft.smart.pos.inventory.constants.InventoryErrorCode;
import com.neusoft.smart.pos.dto.inventory.request.GetSupplierRequest;
import com.neusoft.smart.pos.dto.inventory.request.PostSupplierRequest;
import com.neusoft.smart.pos.dto.inventory.response.SupplierResponse;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryProductSupplier;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryProductSupplierExample;
import com.neusoft.smart.pos.inventory.persistent.domain.InventoryStoreInoutExample;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryProductSupplierMapper;
import com.neusoft.smart.pos.inventory.persistent.mapper.InventoryStoreInoutMapper;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Service("supplierService")
public class SupplierService {
    
    @Resource
    InventoryProductSupplierMapper supplierMapper;
    
    @Resource
    InventoryStoreInoutMapper inventoryStoreInoutMapper;
    
    public List<SupplierResponse> getList(GetSupplierRequest request) {
        InventoryProductSupplierExample example = new InventoryProductSupplierExample();
        InventoryProductSupplierExample.Criteria criteria = example.createCriteria();
        
        example.setOrderByClause("id desc");
        
        criteria.andDeletedEqualTo((byte) 0);
        if (!StringUtils.isEmpty(request.getShopId())) criteria.andShopIdEqualTo(request.getShopId());
        
        if (!StringUtils.isEmpty(request.getName())) {
            criteria.andNameLike("%" + request.getName() + "%");
        }
        if (request.getStatus() != null) criteria.andIsActiveEqualTo(request.getStatus());
        
        List<SupplierResponse> list = new ArrayList<>();
        for (InventoryProductSupplier supplier : supplierMapper.selectByExample(example)) {
            SupplierResponse response = new SupplierResponse();
            response.setId(supplier.getId());
            response.setName(supplier.getName());
            response.setSupplierUser(supplier.getSupplierUser());
            response.setTelephone(supplier.getTelephone());
            response.setEmail(supplier.getEmail());
            response.setContent(supplier.getContent());
            response.setStatus(supplier.getIsActive());
            response.setAddress(supplier.getAddress());
            
            list.add(response);
        }
        
        return list;
    }
    
    public SupplierResponse getDetail(Integer id) {
        InventoryProductSupplier supplier = supplierMapper.selectByPrimaryKey(id);
        SupplierResponse response = new SupplierResponse();
        
        response.setId(supplier.getId());
        response.setName(supplier.getName());
        response.setSupplierUser(supplier.getSupplierUser());
        response.setTelephone(supplier.getTelephone());
        response.setEmail(supplier.getEmail());
        response.setContent(supplier.getContent());
        response.setStatus(supplier.getIsActive());
        response.setAddress(supplier.getAddress());
        
        return response;
    }
    
    public SupplierResponse create(PostSupplierRequest request) {
        InventoryProductSupplierExample example = new InventoryProductSupplierExample();
        InventoryProductSupplierExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(request.getName());
        criteria.andShopIdEqualTo(request.getShopId());
        criteria.andDeletedEqualTo(new Byte("0"));
        if (supplierMapper.countByExample(example) > 0) {
            throw new BusinessCommonException(InventoryErrorCode.DUPLICATE_SUPPLIER_NAME);
        }
        InventoryProductSupplier supplier = new InventoryProductSupplier();
        supplier.setShopId(request.getShopId());
        supplier.setName(validateName(request.getName()));
        supplier.setTelephone(request.getTelephone());
        supplier.setIsActive(request.getStatus());
        supplier.setSupplierUser(request.getSupplierUser());
        supplier.setEmail(request.getEmail());
        supplier.setAddress(request.getAddress());
        supplier.setContent(request.getContent());
        
        supplierMapper.insertSelective(supplier);
        
        SupplierResponse response = new SupplierResponse();
        response.setId(supplier.getId());
        response.setName(supplier.getName());
        response.setSupplierUser(supplier.getSupplierUser());
        response.setTelephone(supplier.getTelephone());
        response.setEmail(supplier.getEmail());
        response.setContent(supplier.getContent());
        response.setStatus(supplier.getIsActive());
        response.setAddress(supplier.getAddress());
        
        return response;
    }
    
    public void update(PostSupplierRequest request) {
        InventoryProductSupplier sup = supplierMapper.selectByPrimaryKey(request.getId());
        if (sup == null) {
            throw new BusinessCommonException(InventoryErrorCode.SUPPLIER_NOT_EXIST);
        }
        InventoryProductSupplierExample example = new InventoryProductSupplierExample();
        InventoryProductSupplierExample.Criteria criteria = example.createCriteria();
        criteria.andNameEqualTo(request.getName());
        criteria.andShopIdEqualTo(sup.getShopId());
        criteria.andDeletedEqualTo(new Byte("0"));
        criteria.andIdNotEqualTo(request.getId());
        if (supplierMapper.countByExample(example) > 0) {
            throw new BusinessCommonException(InventoryErrorCode.DUPLICATE_SUPPLIER_NAME);
        }
        
        InventoryProductSupplier supplier = new InventoryProductSupplier();
        supplier.setId(request.getId());
        if (!StringUtils.isEmpty(request.getName())) supplier.setName(validateName(request.getName()));
        if (!StringUtils.isEmpty(request.getTelephone())) supplier.setTelephone(request.getTelephone());
        if (request.getStatus() != null) supplier.setIsActive(request.getStatus());
        if (!StringUtils.isEmpty(request.getSupplierUser())) supplier.setSupplierUser(request.getSupplierUser());
        if (!StringUtils.isEmpty(request.getEmail())) supplier.setEmail(request.getEmail());
        if (!StringUtils.isEmpty(request.getAddress())) supplier.setAddress(request.getAddress());
        if (!StringUtils.isEmpty(request.getContent())) supplier.setContent(request.getContent());
        
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }
    
    public void delete(Integer id) {
        //查询供应商是否关联库存
        InventoryStoreInoutExample example = new InventoryStoreInoutExample();
        example.createCriteria().andSupplierEqualTo(id);
        if (inventoryStoreInoutMapper.countByExample(example) > 0) {
            throw new BusinessCommonException(InventoryErrorCode.SUPPLIER_NOT_DELETE);
        }
        
        InventoryProductSupplier supplier = new InventoryProductSupplier();
        supplier.setId(id);
        supplier.setDeleted((byte) 1);
        
        supplierMapper.updateByPrimaryKeySelective(supplier);
    }
    
    private String validateName(String name) {
        if (name.length() > 50) {
            throw new BusinessCommonException(InventoryErrorCode.TOO_LONG_SUPPLIER_NAME);
        }
        
        return name;
    }
}
