BasicDataApiService.java 13.4 KB
package com.huaheng.api.general.service;

import com.huaheng.common.exception.service.ServiceException;
import com.huaheng.common.utils.security.ShiroUtils;
import com.huaheng.framework.web.domain.AjaxResult;
import com.huaheng.pc.general.company.domain.Company;
import com.huaheng.pc.general.company.service.ICompanyService;
import com.huaheng.pc.general.customer.domain.Customer;
import com.huaheng.pc.general.customer.service.ICustomerService;
import com.huaheng.pc.general.material.domain.Material;
import com.huaheng.pc.general.material.service.IMaterialService;
import com.huaheng.pc.general.supplier.domain.Supplier;
import com.huaheng.pc.general.supplier.service.ISupplierService;
import com.huaheng.pc.general.warehouse.domain.Warehouse;
import com.huaheng.pc.general.warehouse.service.IWarehouseService;
import com.huaheng.pc.system.dept.domain.Dept;
import com.huaheng.pc.system.dept.service.IDeptService;
import com.huaheng.pc.system.dict.domain.DictData;
import com.huaheng.pc.system.dict.domain.DictType;
import com.huaheng.pc.system.dict.service.IDictDataService;
import com.huaheng.pc.system.dict.service.IDictTypeService;
import com.huaheng.pc.system.user.domain.User;
import com.huaheng.pc.system.user.service.IUserService;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Component
@Transactional
public class BasicDataApiService {
    @Autowired
    IDictTypeService dictTypeService;
    @Autowired
    IDictDataService dictDataService;
    @Autowired
    IMaterialService materialService;


    @Autowired
    IUserService iUserService;

    @Autowired
    IDeptService iDeptService;

    @Autowired
    ICustomerService iCustomerService;

    @Autowired
    IWarehouseService iWarehouseService;

    @Autowired
    ISupplierService iSupplierService;

    @Autowired
    ICompanyService companyService;

    /**
     * 物料类别编码通用接口
     * @param dictData
     * @return
     */
    public AjaxResult dict(DictData dictData) {
        int result = 0;
        //检查字典表,如果没有就添加
        DictData condition = new DictData();
        try {
            condition.setDictType(dictData.getDictType());
            condition.setWarehouseCode(dictData.getWarehouseCode());
            condition.setDictValue(dictData.getDictValue());
            condition.setEnable(true);
            List<DictData> dictDatas = dictDataService.selectDictDataList(dictData);
            if (dictDatas.size() < 1) {
                //找出字典头表的id
                DictType dictType = new DictType();
                dictType.setWarehouseCode(dictData.getWarehouseCode());
                dictType.setDictType(dictData.getDictType());
                List<DictType> dictList = dictTypeService.selectDictTypeList(dictType);
                if (dictList.size() < 1) {
                    dictType.setWarehouseId(dictData.getWarehouseId());
                    dictType.setDeleted(false);
                    dictType.setEnable(true);
                    result = dictTypeService.insertDictType(dictType);
                    dictList.add(dictType);
                    if (result < 1) {
                        throw new ServiceException("新增字典类型失败!");
                    }
                }
                //增新一条字典明细表的记录
                dictData.setHeaderId(dictList.get(0).getId());
                result = dictDataService.insertDictData(dictData);
                if (result < 1) {
                    throw new ServiceException("新增字典数据失败!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
        return AjaxResult.success("新增字典成功");
    }

    /**
     * 检查是否存在物料,如果存在就修改,不存在就新增
     * @param material
     * @return
     */
    @Transactional
    public AjaxResult material(Material material)    {
        int result = 0;
        Material condition = new Material();
        Warehouse warehouse=new Warehouse();
        Company company=new Company();
        String warehouseCode=material.getWarehouseCode();
        if(material.getCode()==null ||warehouseCode ==null ||material.getCompanyCode()==null){
            return AjaxResult.error("数据有误");
        }
        warehouse.setCode(warehouseCode);
        warehouse=iWarehouseService.selectFirstEntity(warehouse);
        if(warehouse==null){
            return AjaxResult.error("没有该仓库");
        }else {
            material.setWarehouseId(warehouse.getId());
        }
        company.setCode(material.getCompanyCode());
        company=companyService.selectFirstEntity(company);
        if(company==null){
            return AjaxResult.error("没有该货主");
        }
        material.setCompanyId(company.getId());
        try {
            condition.setCode(material.getCode());
            condition.setWarehouseCode(warehouseCode);
            Material entity = materialService.selectFirstEntity(condition);
            if (entity == null) {
                result = materialService.insert(material);
                if (result < 1) {
                    throw new ServiceException("新增物料失败!");
                }
            } else {
                material.setId(entity.getId());
                result = materialService.updateByModel(material);
                if (result < 1) {
                    throw new ServiceException("修改物料失败!");
                }
            }
            DictData dictData=new DictData();
            dictData.setHeaderId(17);
            dictData.setWarehouseId(material.getWarehouseId());
            dictData.setWarehouseCode(material.getWarehouseCode());
            dictData.setDictValue(material.getType());
            dictData.setDictLabel(material.getDictLabel());
            dictData.setDictType("materialType");
            this.dict(dictData);
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
        return AjaxResult.success("新增物料成功");
    }



    /**
     * 人员档案通用接口
     * @param user
     * @return
     */
    public AjaxResult user(User user){
        int result = 0;
        User user1 = new User();
        try {
            user1.setLoginName(user.getLoginName());
            //判断系统中是否有该用户,如果有则更新,如果没有则新增
            if(user.getLoginName()==null) {
                return AjaxResult.error("没有人员编码");
            }
                if (iUserService.selectmen(user.getLoginName()) == null) {
                result = iUserService.insertUser(user);
                if (result < 1) {
                    throw new ServiceException("新增人员档案失败!");
                } else {
                    return AjaxResult.success("新增人员档案成功!");
                }
            } else {
                result = iUserService.updateUser(user);
                if (result < 1) {
                    throw new ServiceException("更新人员档案失败!");
                } else {
                    return AjaxResult.success("更新人员档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }



    /**
     * 部门档案通用接口
     * @param dept
     * @return
     */
    public AjaxResult dept(Dept dept){
        int result = 0;
        try {
            Dept rs = iDeptService.selectDepts(dept.getCode());
                if (rs == null) {
                result = iDeptService.insertDepts(dept);
                if (result < 1){
                    throw new ServiceException("新增部门档案失败!");
                }else {
                    return AjaxResult.success("新增部门档案成功!");
                }
            }else {
                dept.setId(rs.getId());
                result = iDeptService.updateDept(dept);
                if (result < 1){
                    throw new ServiceException("更新部门档案失败!");
                }else {
                    return AjaxResult.success("更新部门档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }

    /**
     * 客户档案通用接口
     * @param customer
     * @return
     */
    public AjaxResult customer(Customer customer){
        int result = 0;
        Customer customer1 = new Customer();
        try {
            if(customer.getCode()==null) {
                return AjaxResult.error("没有客户代码");
            }
            Warehouse warehouse=new Warehouse();
            warehouse.setCode(customer.getWarehouseCode());
            warehouse=iWarehouseService.selectFirstEntity(warehouse);
            if(warehouse==null || customer.getWarehouseCode()==null){
                return AjaxResult.error("没有该仓库");
            }else {
                customer.setWarehouseId(warehouse.getId());
            }
                customer1.setCode(customer.getCode());
                customer1.setWarehouseCode(customer.getWarehouseCode());
            Customer rs = iCustomerService.selectFirstEntity(customer1);
            if ( rs == null) {
                result = iCustomerService.insert(customer);
                if (result < 1) {
                    throw new ServiceException("新增客户档案失败!");
                } else {
                    return AjaxResult.success("新增客户档案成功!");
                }
            } else {
                customer.setId(rs.getId());
                result = iCustomerService.updateByModel(customer);
                if (result < 1) {
                    throw new ServiceException("更新客户档案失败!");
                } else {
                    return AjaxResult.success("更新客户档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }

    /**
     * 仓库档案通用接口
     * @param warehouse
     * @return
     */
    public AjaxResult warehouse(Warehouse warehouse){
        int result = 0;
        Warehouse warehouse1 = new Warehouse();
        try {
            Company company= companyService.selectEntityById(warehouse.getCompanyId());
            if(company==null){
                return AjaxResult.error("没有该货主");
            }
            if(warehouse.getCode()==null) {
                return AjaxResult.error("没有code");
            }
            warehouse1.setCode(warehouse.getCode());
            //判断系统中是否有该仓库,若有则更新,若无则新增
            Warehouse rs = iWarehouseService.selectFirstEntity(warehouse1);
            if (rs == null) {
                result = iWarehouseService.insertWarehouseAndDict(warehouse);
                if (result < 1) {
                    throw new ServiceException("新增仓库档案失败!");
                } else {
                    return AjaxResult.success("新增仓库档案成功!");
                }
            } else {
                warehouse.setId(rs.getId());
                result = iWarehouseService.updateByModel(warehouse);
                if (result < 1) {
                    throw new ServiceException("更新仓库档案失败!");
                } else {
                    return AjaxResult.success("更新仓库档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }

    /**
     * 供应商档案通用接口
     * @param supplier
     * @return
     */
    public AjaxResult supplier(Supplier supplier){
        Warehouse warehouse=new Warehouse();
        warehouse.setCode(supplier.getWarehouseCode());
        warehouse=iWarehouseService.selectFirstEntity(warehouse);
        if(warehouse==null || supplier.getWarehouseCode()==null){
            return AjaxResult.error("没有该仓库");
        }else {
            supplier.setWarehouseId(warehouse.getId());
        }
        if(supplier.getCode()==null){
            return AjaxResult.error("没有供应商代码");
        }
        int result = 0;
        try {
            Supplier supplierB=new Supplier();
            supplierB.setCode(supplier.getCode());
            supplierB.setWarehouseCode(supplier.getWarehouseCode());
            supplierB=iSupplierService.selectFirstEntity(supplierB);
            if (supplierB== null) {
                result = iSupplierService.insert(supplier);
                if (result < 1) {
                    throw new ServiceException("新增供应商失败!");
                } else {
                    return AjaxResult.success("新增供应商成功!");
                }
            } else {
                supplier.setId(supplierB.getId());
                result = iSupplierService.updateByModel(supplier);
                if (result < 1) {
                    throw new ServiceException("更新供应商失败!");
                } else {
                    return AjaxResult.success("更新供应商成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("数据问题。。。");
        }
    }
}