BasicDataApiService.java 12.7 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.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.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;

    /**
     * 物料类别编码通用接口
     * @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("materialType");
                List<DictType> dictList = dictTypeService.selectDictTypeList(dictType);
                if (dictList.size() < 1) {
                    dictType.setWarehouseId(dictData.getWarehouseId());
                    dictType.setDeleted(false);
                    dictType.setEnable(true);
                    result = dictTypeService.insertDictType(dictType);
                    if (result < 1) {
                        throw new ServiceException("新增物料类型失败!");
                    }
                }
                //增新一条字典明细表的记录
                dictData.setHeaderId(dictList.get(0).getId());
                result = dictDataService.insertDictData(dictData);
                if (result < 1) {
                    throw new ServiceException("新增物料类型失败!");
                }
            } else {
                result = dictDataService.updateDictData(dictData);
                if (result < 1) {
                    throw new ServiceException("修改物料类型失败!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("U8数据问题。。。");
        }

        return AjaxResult.success("成功");
    }

    /**
     * 检查是否存在物料,如果存在就修改,不存在就新增
     * @param material
     * @return
     */
    public AjaxResult material(Material material)    {
        int result = 0;
        Material condition = new Material();
        try {
            condition.setCode(material.getCode());
            condition.setWarehouseCode(material.getWarehouseCode());
            Material entity = materialService.selectFirstEntity(condition);
            if (entity == null) {
                material.setCreatedBy(ShiroUtils.getLoginName());
                material.setCreated(new Date());
                result = materialService.insert(material);
                if (result < 1) {
                    throw new ServiceException("新增物料类型失败!");
                }
            } else {
                material.setId(entity.getId());
                entity.setCreatedBy(entity.getCreatedBy());
                entity.setCreated(entity.getCreated());
                result = materialService.updateByModel(material);
                if (result < 1) {
                    throw new ServiceException("修改物料类型失败!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("U8数据问题。。。");
        }
        return AjaxResult.success("成功");
    }



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


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

    /**
     * 客户档案通用接口
     * @param customer
     * @return
     */
    public AjaxResult customer(Customer customer){
        int result = 0;
        Customer customer1 = new Customer();
        try {

            customer1.setCode(customer.getCode());
            Customer customer2=iCustomerService.selectFirstEntity(customer1);
            customer1.setWarehouseId(customer.getWarehouseId());
            customer1.setName(customer.getName());
            customer1.setAttentionTo(customer.getAttentionTo());
            customer1.setMobile(customer.getMobile());
            if ( customer2 == null) {
                customer1.setCreated(customer.getCreated());
                customer1.setCreatedBy(customer.getCreatedBy());
                result = iCustomerService.insert(customer1);
                if (result < 1) {
                    throw new ServiceException("新增客户档案失败!");
                } else {
                    return AjaxResult.success("新增客户档案成功!");
                }
            } else {
                customer1.setLastUpdated(customer.getLastUpdated());
                customer1.setLastUpdatedBy(customer.getLastUpdatedBy());
                result = iCustomerService.updateByModel(customer1);
                if (result < 1) {
                    throw new ServiceException("更新客户档案失败!");
                } else {
                    return AjaxResult.success("更新客户档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("U8数据问题。。。");
        }
    }

    /**
     * 仓库档案通用接口
     * @param warehouse
     * @return
     */
    public AjaxResult warehouse(Warehouse warehouse){
        int result = 0;
        Warehouse warehouse1 = new Warehouse();
        try {
            warehouse1.setCode(warehouse.getCode());
            warehouse1.setName(warehouse.getName());
            warehouse1.setCompanyId(warehouse.getCompanyId());
            warehouse1.setUserDef1(warehouse.getUserDef1());
            //判断系统中是否有该仓库,若有则更新,若无则新增
            if (iWarehouseService.selectFirstEntity(warehouse) == null) {
                warehouse1.setCreated(warehouse.getCreated());
                warehouse1.setCreatedBy(warehouse.getCreatedBy());
                result = iWarehouseService.insertWarehouseAndDict(warehouse1);
                if (result < 1) {
                    throw new ServiceException("新增仓库档案失败!");
                } else {
                    return AjaxResult.success("新增仓库档案成功!");
                }
            } else {
                warehouse1.setLastUpdatedBy(warehouse.getLastUpdatedBy());
                warehouse1.setLastUpdated(warehouse.getLastUpdated());
                result = iWarehouseService.updateByModel(warehouse1);
                if (result < 1) {
                    throw new ServiceException("更新仓库档案失败!");
                } else {
                    return AjaxResult.success("更新仓库档案成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("U8数据问题。。。");
        }
    }

    /**
     * 供应商档案通用接口
     * @param supplier
     * @return
     */
    public AjaxResult supplier(Supplier supplier){
        int result = 0;
        Supplier supplier1 = new Supplier();
        try {
            supplier1.setWarehouseId(supplier.getWarehouseId());
            supplier1.setWarehouseCode(supplier.getWarehouseCode());
            supplier1.setCode(supplier.getCode());
            supplier1.setName(supplier.getName());
            supplier1.setPhoneNum(supplier.getPhoneNum());
            supplier1.setMobile(supplier.getMobile());
            supplier1.setUserDef1(supplier.getUserDef1());
            if (iSupplierService.selectcode(supplier.getCode()) == null) {
                supplier1.setCreated(supplier.getCreateTime());
                supplier1.setCreatedBy(supplier.getCreateBy());
                result = iSupplierService.insert(supplier1);
                if (result < 1) {
                    throw new ServiceException("新增供应商失败!");
                } else {
                    return AjaxResult.success("新增供应商成功!");
                }
            } else {
                supplier1.setLastUpdated(supplier.getLastUpdated());
                supplier1.setLastUpdatedBy(supplier.getLastUpdatedBy());
                result = iSupplierService.updateByModel(supplier1);
                if (result < 1) {
                    throw new ServiceException("更新供应商失败!");
                } else {
                    return AjaxResult.success("更新供应商成功!");
                }
            }
        }catch (Exception e){
            throw new ServiceException("U8数据问题。。。");
        }
    }
}