| | |
| | | import org.springframework.stereotype.Service; |
| | | import org.springframework.transaction.annotation.Transactional; |
| | | |
| | | import javax.annotation.Resource; |
| | | import java.util.List; |
| | | |
| | | /** |
| | |
| | | @Service |
| | | public class SysCompanyServiceImpl extends ServiceImpl<SysCompanyMapper, SysCompany> implements SysCompanyService { |
| | | |
| | | @Resource |
| | | private SysCompanyMapper companyMapper; |
| | | |
| | | @Override |
| | | public CommonPage selectCompanyList(SysCompany company) { |
| | |
| | | if(!checkNameUnique(company)){ |
| | | throw new ApiException("公司名称已存在"); |
| | | } |
| | | if(!checkCodeUnique(company)){ |
| | | throw new ApiException("公司编码已存在"); |
| | | } |
| | | company.setCode(company.getCode().toUpperCase()); |
| | | company.setCreateBy(SecurityUtils.getUsername()); |
| | | int row= baseMapper.insert(company); |
| | | if(row<1){ |
| | | throw new ApiException("新增公司失败"); |
| | | } |
| | | |
| | | return row; |
| | | } |
| | | |
| | |
| | | if(!checkNameUnique(company)){ |
| | | throw new ApiException("公司名称已存在"); |
| | | } |
| | | if(!checkCodeUnique(company)){ |
| | | throw new ApiException("公司编码已存在"); |
| | | } |
| | | company.setCode(company.getCode().toUpperCase()); |
| | | company.setUpdateBy(SecurityUtils.getUsername()); |
| | | return baseMapper.updateById(company); |
| | | } |
| | |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public boolean checkNameUnique(SysCompany company){ |
| | | Long companyId=company.getId()==null?-1L:company.getId(); |
| | | SysCompany com= baseMapper.checkNameUnique(company.getName()); |
| | |
| | | } |
| | | return UserConstant.UNIQUE; |
| | | } |
| | | |
| | | |
| | | @Override |
| | | public void createProductTables(Long companyId) { |
| | | // 这里可以使用 MyBatis-Plus 的动态 SQL 或者直接执行 SQL 语句来创建表 |
| | | // 为了简化示例,我们直接执行 SQL 语句 |
| | | String createProductCategoryTableSql = "CREATE TABLE IF NOT EXISTS product_category_demo"+"_"+companyId+" (" + |
| | | "id BIGINT AUTO_INCREMENT PRIMARY KEY, " + |
| | | "name VARCHAR(255) NOT NULL, " + |
| | | "company_id BIGINT NOT NULL)"; |
| | | String createProductTableSql = "CREATE TABLE IF NOT EXISTS product_demo"+"_"+companyId+" (" + |
| | | "id BIGINT AUTO_INCREMENT PRIMARY KEY, " + |
| | | "name VARCHAR(255) NOT NULL, " + |
| | | "description TEXT, " + |
| | | "price DECIMAL(10, 2) NOT NULL, " + |
| | | "category_id BIGINT NOT NULL)"; |
| | | |
| | | // 执行 SQL 语句 |
| | | companyMapper.execute(createProductCategoryTableSql); |
| | | companyMapper.execute(createProductTableSql); |
| | | } |
| | | |
| | | |
| | | public boolean checkCodeUnique(SysCompany company){ |
| | | Long companyId=company.getId()==null?-1L:company.getId(); |
| | | SysCompany com= baseMapper.checkCodeUnique(company.getCode()); |
| | | if(com!=null&&com.getId().longValue()!=companyId.longValue()){ |
| | | return UserConstant.NOT_UNIQUE; |
| | | } |
| | | return UserConstant.UNIQUE; |
| | | } |
| | | } |