|
|
@@ -0,0 +1,316 @@
|
|
|
+package com.usoftchina.saas.document.service.impl;
|
|
|
+
|
|
|
+import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.usoftchina.saas.account.api.CompanyApi;
|
|
|
+import com.usoftchina.saas.base.service.CommonBaseServiceImpl;
|
|
|
+import com.usoftchina.saas.commons.api.MessageLogService;
|
|
|
+import com.usoftchina.saas.commons.dto.BatchDealBaseDTO;
|
|
|
+import com.usoftchina.saas.commons.dto.DocBaseDTO;
|
|
|
+import com.usoftchina.saas.commons.dto.ListReqDTO;
|
|
|
+import com.usoftchina.saas.commons.exception.BizExceptionCode;
|
|
|
+import com.usoftchina.saas.commons.po.DataImportDetail;
|
|
|
+import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
+import com.usoftchina.saas.document.entities.*;
|
|
|
+import com.usoftchina.saas.document.entities.CustAramount;
|
|
|
+import com.usoftchina.saas.document.entities.CustAramountList;
|
|
|
+import com.usoftchina.saas.document.mapper.*;
|
|
|
+import com.usoftchina.saas.document.service.CustAramountService;
|
|
|
+import com.usoftchina.saas.document.service.CustAramountService;
|
|
|
+import com.usoftchina.saas.exception.BizException;
|
|
|
+import com.usoftchina.saas.page.PageRequest;
|
|
|
+import com.usoftchina.saas.utils.CollectionUtils;
|
|
|
+import org.slf4j.Logger;
|
|
|
+import org.slf4j.LoggerFactory;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.text.ParseException;
|
|
|
+import java.text.SimpleDateFormat;
|
|
|
+import java.util.*;
|
|
|
+
|
|
|
+/**
|
|
|
+ * @author hey
|
|
|
+ * @Date 2019/03/11
|
|
|
+ */
|
|
|
+@Service
|
|
|
+public class CustAramountServiceImpl extends CommonBaseServiceImpl<CustAramountMapper, CustAramount> implements CustAramountService {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private MessageLogService messageLogService;
|
|
|
+ @Autowired
|
|
|
+ private CustomerMapper customerMapper;
|
|
|
+ @Autowired
|
|
|
+ private DataImportMapper dataImportMapper;
|
|
|
+ @Autowired
|
|
|
+ private CompanyApi companyApi;
|
|
|
+
|
|
|
+ private static final Logger LOGGER = LoggerFactory.getLogger(CustAramountServiceImpl.class);
|
|
|
+
|
|
|
+ public PageInfo<CustAramountList> getListDataByCondition(PageRequest page, ListReqDTO listReqDTO){
|
|
|
+ //设置分页
|
|
|
+ if (null == page || page.getSize() == 0 || page.getNumber() == 0) {
|
|
|
+ page = new PageRequest();
|
|
|
+ page.setNumber(1);
|
|
|
+ page.setSize(10);
|
|
|
+ }
|
|
|
+ PageHelper.startPage(page.getNumber(), page.getSize());
|
|
|
+ //取分页信息
|
|
|
+ String condition = listReqDTO.getFinalCondition();
|
|
|
+ List<CustAramountList> list = getMapper().getListDataByCondition(condition, BaseContextHolder.getCompanyId());
|
|
|
+ PageInfo<CustAramountList> pageInfo = new PageInfo<CustAramountList>(list);
|
|
|
+ return pageInfo;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public boolean batchDelete(BatchDealBaseDTO baseDTOs) {
|
|
|
+ if (null == baseDTOs || null == baseDTOs.getBaseDTOs() ||
|
|
|
+ baseDTOs.getBaseDTOs().size() == 0) {
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+ for (DocBaseDTO base : baseDTOs.getBaseDTOs()) {
|
|
|
+ deleteById(base.getId());
|
|
|
+ }
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+
|
|
|
+ public void deleteById(Long id) {
|
|
|
+ if(id != null && id > 0){
|
|
|
+ CustAramount custAramount = getMapper().getCustAramount(id,BaseContextHolder.getCompanyId());
|
|
|
+ validMoney(custAramount);
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
|
|
|
+ //判断本次期间是否已结账,结账了就不能删除
|
|
|
+ int checkBeginData = getMapper().checkBeginData(sdf.format(custAramount.getCa_begindate()),BaseContextHolder.getCompanyId());
|
|
|
+ if(checkBeginData > 0){
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_NOPERIOD_DELETE);
|
|
|
+ }
|
|
|
+
|
|
|
+ getMapper().deleteByPrimaryKey(id);
|
|
|
+ //记录LOG
|
|
|
+ messageLogService.delete(generateMsgObj(id));
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public void validMoney(CustAramount custAramount){
|
|
|
+ //校验是否可以修改或新增客户期初
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("v_id",custAramount.getCa_custid());//客户ID
|
|
|
+ map.put("v_code", custAramount.getCa_custcode());//客户编号
|
|
|
+ map.put("v_type","custCurrency");
|
|
|
+ map.put("v_currency",custAramount.getCa_currency());
|
|
|
+ map.put("v_companyid",BaseContextHolder.getCompanyId());
|
|
|
+ map.put("v_res","");
|
|
|
+ getMapper().check(map);
|
|
|
+ Object result = map.get("v_res");
|
|
|
+ if(!StringUtils.isEmpty(result)){
|
|
|
+ throw new BizException(-999999,result.toString());
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ public DocBaseDTO saveData(CustAramount custAramount){
|
|
|
+ DocBaseDTO docBaseDTO = null;
|
|
|
+ if(custAramount.getCa_recamount()==null){custAramount.setCa_recamount(0.00);}
|
|
|
+ if(custAramount.getCa_beginaramount()==null){custAramount.setCa_beginaramount(0.00);}
|
|
|
+ if(custAramount.getCa_beginprerecamount()==null){custAramount.setCa_beginprerecamount(0.00);}
|
|
|
+ if(custAramount.getCa_preamount()==null){custAramount.setCa_preamount(0.00);}
|
|
|
+ //判断是否已存在数据
|
|
|
+ boolean hasData = hasData(custAramount.getCa_custcode(),custAramount.getCa_currency(),custAramount.getId());
|
|
|
+ if(hasData){
|
|
|
+ validMoney(custAramount);
|
|
|
+ }
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
|
|
|
+ //更新余额
|
|
|
+ custAramount.setCa_leftamount(custAramount.getCa_beginaramount()-custAramount.getCa_beginprerecamount()+custAramount.getCa_recamount()-custAramount.getCa_preamount());
|
|
|
+
|
|
|
+ if(custAramount.getId() == 0){
|
|
|
+ //新增
|
|
|
+ custAramount.setCompanyId(BaseContextHolder.getCompanyId());
|
|
|
+ custAramount.setCreatorId(BaseContextHolder.getUserId());
|
|
|
+ custAramount.setCreateTime(new Date());
|
|
|
+ custAramount.setCreatorName(BaseContextHolder.getUserName());
|
|
|
+
|
|
|
+ //获取当前期间
|
|
|
+ String now = getMapper().selectPeriod(BaseContextHolder.getCompanyId());
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(now);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
|
|
|
+ custAramount.setCa_begindate(calendar.getTime());
|
|
|
+
|
|
|
+ getMapper().insertSelective(custAramount);
|
|
|
+ //记录LOG
|
|
|
+ docBaseDTO = generateMsgObj(custAramount.getId());
|
|
|
+ messageLogService.save(docBaseDTO);
|
|
|
+ }else{
|
|
|
+ //修改
|
|
|
+ custAramount.setUpdaterId(BaseContextHolder.getUserId());
|
|
|
+ custAramount.setUpdateTime(new Date());
|
|
|
+ //期初日期是否已结账
|
|
|
+ int checkBeginData = getMapper().checkBeginData(sdf.format(custAramount.getCa_begindate()),BaseContextHolder.getCompanyId());
|
|
|
+ if(checkBeginData > 0){
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_NOPERIOD_UPDATE);
|
|
|
+ }
|
|
|
+ getMapper().updateByPrimaryKeySelective(custAramount);
|
|
|
+ //记录LOG
|
|
|
+ docBaseDTO = generateMsgObj(custAramount.getId());
|
|
|
+ messageLogService.update(docBaseDTO);
|
|
|
+ }
|
|
|
+ //调用更新
|
|
|
+ customerMapper.updateCustleftamountByPrimaryKey(custAramount.getCa_custid());
|
|
|
+ return docBaseDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ private boolean hasData(String custCode, String currency,Long id){
|
|
|
+ int hasCustCode = getMapper().hasCustCode(custCode, BaseContextHolder.getCompanyId());
|
|
|
+ if (hasCustCode == 0){
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_NOCUST_SAVE);
|
|
|
+ }
|
|
|
+ int hasCurrency = getMapper().hasCurrency(currency, BaseContextHolder.getCompanyId());
|
|
|
+ if (hasCurrency == 0){
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_NOCURRENCY_SAVE);
|
|
|
+ }
|
|
|
+ int count = getMapper().validCustCodeAndCurrency(custCode, BaseContextHolder.getCompanyId(), currency,id);
|
|
|
+ if (count > 0){
|
|
|
+ return true;
|
|
|
+ }
|
|
|
+ return false;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * 构造 记录日志对象
|
|
|
+ * @param id
|
|
|
+ * @return
|
|
|
+ */
|
|
|
+ private DocBaseDTO generateMsgObj(Long id){
|
|
|
+ return new DocBaseDTO(id, "", "CustAramount");
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * String转Date类型
|
|
|
+ *
|
|
|
+ * @param dateStr
|
|
|
+ * 时间字符串
|
|
|
+ * @return Date类型时间
|
|
|
+ * @throws Exception
|
|
|
+ * 异常
|
|
|
+ */
|
|
|
+ public static Date string2Date(String dateStr, String format) throws Exception {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ if (dateStr != null) {
|
|
|
+ return sdf.parse(dateStr);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Date转String
|
|
|
+ *
|
|
|
+ * @param date
|
|
|
+ * Date类型时间
|
|
|
+ * @return 时间字符串
|
|
|
+ */
|
|
|
+ public static String date2String(Date date, String format) {
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat(format);
|
|
|
+ if (date != null) {
|
|
|
+ return sdf.format(date);
|
|
|
+ }
|
|
|
+ return null;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void saveToFormal(Integer id, boolean update) {
|
|
|
+ if (null == id || "0".equals(id)) {
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ Long companyId = BaseContextHolder.getCompanyId();
|
|
|
+ StringBuilder err = new StringBuilder();
|
|
|
+ List<CustAramount> list = new ArrayList<>();
|
|
|
+ CustAramount listDTO = null;
|
|
|
+ List<DataImportDetail> details = dataImportMapper.selectDataById(id);
|
|
|
+ if (!CollectionUtils.isEmpty(details)) {
|
|
|
+ int i = 0;
|
|
|
+ for (DataImportDetail detail : details) {
|
|
|
+ //数据验证
|
|
|
+ if (StringUtils.isEmpty(detail)) {
|
|
|
+ throw new BizException(BizExceptionCode.BIZ_REPORT_NOTCORRECT);
|
|
|
+ }
|
|
|
+ CustAramount custAramount = JSONObject.parseObject(detail.getDd_maindata(), CustAramount.class);
|
|
|
+ if(custAramount.getId()==null){custAramount.setId(0L);}
|
|
|
+ if(custAramount.getCa_recamount()==null){custAramount.setCa_recamount(0.00);}
|
|
|
+ if(custAramount.getCa_beginaramount()==null){custAramount.setCa_beginaramount(0.00);}
|
|
|
+ if(custAramount.getCa_beginprerecamount()==null){custAramount.setCa_beginprerecamount(0.00);}
|
|
|
+ if(custAramount.getCa_preamount()==null){custAramount.setCa_preamount(0.00);}
|
|
|
+ //判断是否已存在数据
|
|
|
+ List<Customer> customers = customerMapper.getCustomersByCondition("cu_statuscode = 'ENABLE' AND cu_code = '"+custAramount.getCa_custcode()+"'",BaseContextHolder.getCompanyId());
|
|
|
+ if (customers.size() == 0){
|
|
|
+ err.append("第" + (i + 3) + "行 : 客户编号不存在或该客户状态为禁用!<br/> ");
|
|
|
+ }else{
|
|
|
+ int hasCustNameByCode = getMapper().hasCustNameByCode(custAramount.getCa_custcode(),BaseContextHolder.getCompanyId(),custAramount.getCa_custname());
|
|
|
+ if(hasCustNameByCode==0){
|
|
|
+ err.append("第" + (i + 3) + "行 : 客户名称与编号不匹配!<br/> ");
|
|
|
+ }
|
|
|
+ custAramount.setCa_custid(customers.get(0).getId());
|
|
|
+ }
|
|
|
+ int hasCurrency = getMapper().hasCurrency(custAramount.getCa_currency(), BaseContextHolder.getCompanyId());
|
|
|
+ if (hasCurrency == 0){
|
|
|
+ err.append("第" + (i + 3) + "行 : 币别不存在!<br/> ");
|
|
|
+ }
|
|
|
+ int count = getMapper().validCustCodeAndCurrency(custAramount.getCa_custcode(), BaseContextHolder.getCompanyId(), custAramount.getCa_currency(),0L);
|
|
|
+ if (count > 0){
|
|
|
+ //校验是否可以修改或新增客户期初
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("v_id",custAramount.getCa_custid());//客户ID
|
|
|
+ map.put("v_code", custAramount.getCa_custcode());//客户编号
|
|
|
+ map.put("v_type","custCurrency");
|
|
|
+ map.put("v_currency",custAramount.getCa_currency());
|
|
|
+ map.put("v_companyid",BaseContextHolder.getCompanyId());
|
|
|
+ map.put("v_res","");
|
|
|
+ getMapper().check(map);
|
|
|
+ Object result = map.get("v_res");
|
|
|
+ if(!StringUtils.isEmpty(result)){
|
|
|
+ err.append("第" + (i + 3) + "行 : "+result.toString()+"<br/> ");
|
|
|
+ }
|
|
|
+ }
|
|
|
+ list.add(custAramount);
|
|
|
+ i++;
|
|
|
+ }
|
|
|
+ if (err.length() > 0) {
|
|
|
+ dataImportMapper.updateDataImportError(err.toString(), id);
|
|
|
+ throw new BizException(12345, err.toString());
|
|
|
+ }
|
|
|
+ for (CustAramount custAramount : list) {
|
|
|
+ //新增
|
|
|
+ custAramount.setCompanyId(BaseContextHolder.getCompanyId());
|
|
|
+ custAramount.setCreatorId(BaseContextHolder.getUserId());
|
|
|
+ custAramount.setCreateTime(new Date());
|
|
|
+ custAramount.setCreatorName(BaseContextHolder.getUserName());
|
|
|
+ SimpleDateFormat sdf = new SimpleDateFormat ("yyyyMM");
|
|
|
+ //更新余额
|
|
|
+ custAramount.setCa_leftamount(custAramount.getCa_beginaramount()-custAramount.getCa_beginprerecamount()+custAramount.getCa_recamount()-custAramount.getCa_preamount());
|
|
|
+ //获取当前期间
|
|
|
+ String now = getMapper().selectPeriod(BaseContextHolder.getCompanyId());
|
|
|
+ Date date = null;
|
|
|
+ try {
|
|
|
+ date = sdf.parse(now);
|
|
|
+ } catch (ParseException e) {
|
|
|
+ e.printStackTrace();
|
|
|
+ }
|
|
|
+ Calendar calendar = Calendar.getInstance();
|
|
|
+ calendar.setTime(date);
|
|
|
+ calendar.set(Calendar.DAY_OF_MONTH,1);//设置为1号,当前日期既为本月第一天
|
|
|
+ custAramount.setCa_begindate(calendar.getTime());
|
|
|
+ getMapper().insertSelective(custAramount);
|
|
|
+ customerMapper.updateCustleftamountByPrimaryKey(custAramount.getCa_custid());
|
|
|
+ }
|
|
|
+ dataImportMapper.updateDataImport(id);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|