|
|
@@ -0,0 +1,371 @@
|
|
|
+package com.usoftchina.saas.sale.service.impl;
|
|
|
+
|
|
|
+import com.github.pagehelper.PageHelper;
|
|
|
+import com.github.pagehelper.PageInfo;
|
|
|
+import com.usoftchina.saas.base.Result;
|
|
|
+import com.usoftchina.saas.base.service.CommonBaseServiceImpl;
|
|
|
+import com.usoftchina.saas.commons.api.MaxnumberService;
|
|
|
+import com.usoftchina.saas.commons.dto.BatchDealBaseDTO;
|
|
|
+import com.usoftchina.saas.commons.dto.DocBaseDTO;
|
|
|
+import com.usoftchina.saas.commons.dto.DocSavedDTO;
|
|
|
+import com.usoftchina.saas.commons.exception.BizExceptionCode;
|
|
|
+import com.usoftchina.saas.commons.po.Maxnumbers;
|
|
|
+import com.usoftchina.saas.context.BaseContextHolder;
|
|
|
+import com.usoftchina.saas.document.api.WarehouseApi;
|
|
|
+import com.usoftchina.saas.exception.BizException;
|
|
|
+import com.usoftchina.saas.exception.ExceptionCode;
|
|
|
+import com.usoftchina.saas.page.PageRequest;
|
|
|
+import com.usoftchina.saas.sale.dto.ProdIODetailDTO;
|
|
|
+import com.usoftchina.saas.sale.dto.ProdInOutDTO;
|
|
|
+import com.usoftchina.saas.sale.dto.ProdInOutFormDTO;
|
|
|
+import com.usoftchina.saas.sale.dto.ProdInOutReqDTO;
|
|
|
+import com.usoftchina.saas.sale.mapper.*;
|
|
|
+import com.usoftchina.saas.sale.service.ProdInOutService;
|
|
|
+import com.usoftchina.saas.storage.entities.ProdIODetail;
|
|
|
+import com.usoftchina.saas.storage.entities.ProdIODetailExample;
|
|
|
+import com.usoftchina.saas.storage.entities.ProdInOut;
|
|
|
+import com.usoftchina.saas.storage.entities.ProdInOutList;
|
|
|
+import com.usoftchina.saas.utils.BeanMapper;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
+import org.springframework.stereotype.Service;
|
|
|
+import org.springframework.util.StringUtils;
|
|
|
+
|
|
|
+import java.util.*;
|
|
|
+@Service
|
|
|
+public class ProdInOutServiceImpl extends CommonBaseServiceImpl<ProdInOutMapper, ProdInOut> implements ProdInOutService{
|
|
|
+
|
|
|
+// @Autowired
|
|
|
+// private SaledetailMapper saledetailMapper;
|
|
|
+// @Autowired
|
|
|
+// private MaxnumberService maxnumberService;
|
|
|
+ @Autowired
|
|
|
+ private SaleMapper saleMapper;
|
|
|
+ @Autowired
|
|
|
+ private ProdInOutListMapper prodInOutListMapper;
|
|
|
+ @Autowired
|
|
|
+ private ProdInOutMapper prodInOutMapper;
|
|
|
+ @Autowired
|
|
|
+ private ProdIODetailMapper prodIODetailMapper;
|
|
|
+// @Autowired
|
|
|
+// private WarehouseApi warehouseApi;
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public ProdInOutFormDTO getFormData(Long id) {
|
|
|
+ ProdInOutFormDTO prodInOutFormDTO = new ProdInOutFormDTO();
|
|
|
+ //查询主表
|
|
|
+ ProdInOut prodInOut = getMapper().selectByPrimaryKey(id);
|
|
|
+ //将prodInOut实体对象转化为传输对象
|
|
|
+ ProdInOutDTO main = BeanMapper.map(prodInOut,ProdInOutDTO.class);
|
|
|
+ //查询从表
|
|
|
+
|
|
|
+ List<ProdIODetail> prodIODetails = prodIODetailMapper.selectByFK(id);
|
|
|
+ List<ProdIODetailDTO> items = BeanMapper.mapList(prodIODetails,ProdIODetailDTO.class);
|
|
|
+ prodInOutFormDTO.setMain(main);
|
|
|
+ prodInOutFormDTO.setItems(items);
|
|
|
+ return prodInOutFormDTO;
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public DocSavedDTO saveFormData(ProdInOutFormDTO formdata, Boolean isbfaudit) {
|
|
|
+
|
|
|
+ if (null == formdata || null == formdata.getMain()){
|
|
|
+ throw new BizException(500, "数据为空,请填写后再保存");
|
|
|
+ }
|
|
|
+ //公司ID
|
|
|
+ Long companyId = BaseContextHolder.getCompanyId();
|
|
|
+ //人员Id
|
|
|
+ Long userId = BaseContextHolder.getUserId();
|
|
|
+ //获取主表信息
|
|
|
+ ProdInOutDTO main = formdata.getMain();
|
|
|
+ List<ProdIODetailDTO> items = formdata.getItems();
|
|
|
+ //插入从表数据
|
|
|
+ List<ProdIODetail> insertDetails = new ArrayList<>();
|
|
|
+ //更新从表数据
|
|
|
+ List<ProdIODetail> updateDetails = new ArrayList<>();
|
|
|
+ DocSavedDTO saveDTO = new DocSavedDTO();
|
|
|
+ Long pi_id = main.getId();
|
|
|
+ String pi_inoutno = main.getPi_inoutno();
|
|
|
+ String pi_class = main.getPi_class();
|
|
|
+
|
|
|
+ ProdInOut prodInOut = BeanMapper.map(main,ProdInOut.class);
|
|
|
+ prodInOut.setCompanyId(companyId);
|
|
|
+ prodInOut.setCreatorId(userId);
|
|
|
+ prodInOut.setCreateTime(new Date());
|
|
|
+ prodInOut.setPi_date(new Date());
|
|
|
+
|
|
|
+ prodInOut.setPi_class(main.getPi_class());
|
|
|
+ prodInOut.setPi_said(main.getPi_said());
|
|
|
+ prodInOut.setPi_sacode(main.getPi_sacode());
|
|
|
+
|
|
|
+ //编号获取
|
|
|
+ pi_inoutno = pushMaxnubmer(pi_inoutno, pi_id,pi_class);
|
|
|
+ prodInOut.setPi_inoutno(pi_inoutno);
|
|
|
+ saveDTO.setCode(pi_inoutno);
|
|
|
+ //判断更新与保存动作
|
|
|
+ if (StringUtils.isEmpty(pi_id) || "0".equals(pi_id.toString())){
|
|
|
+ //插入操作
|
|
|
+ getMapper().insertSelective(prodInOut);
|
|
|
+ pi_id = prodInOut.getId();
|
|
|
+ //添加从表传输对象
|
|
|
+ for (ProdIODetailDTO item : items) {
|
|
|
+ ProdIODetail detail = BeanMapper.map(item,ProdIODetail.class);
|
|
|
+ detail.setPd_piid(pi_id);
|
|
|
+ detail.setPd_inoutno(pi_inoutno);
|
|
|
+ detail.setPd_piclass(pi_class);
|
|
|
+ detail.setPd_yqty(0.0);
|
|
|
+ insertDetails.add(detail);
|
|
|
+ }
|
|
|
+ //插入从表
|
|
|
+ if (insertDetails.size()>0) {
|
|
|
+ prodIODetailMapper.batchInsert(insertDetails);
|
|
|
+ }
|
|
|
+ saveDTO.setId(pi_id);
|
|
|
+ return saveDTO;
|
|
|
+ }
|
|
|
+ //更新操作
|
|
|
+ getMapper().updateByPrimaryKeySelective(prodInOut);
|
|
|
+ //添加从表传输对象
|
|
|
+ for (ProdIODetailDTO item : items) {
|
|
|
+ ProdIODetail detail = BeanMapper.map(item, ProdIODetail.class);
|
|
|
+ detail.setPd_piid(pi_id);
|
|
|
+ detail.setPd_inoutno(pi_inoutno);
|
|
|
+ detail.setPd_piclass(prodInOut.getPi_class());
|
|
|
+ if (StringUtils.isEmpty(detail.getId()) || "0".equals(detail.getId().toString())) {
|
|
|
+ insertDetails.add(detail);
|
|
|
+ } else {
|
|
|
+ updateDetails.add(detail);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //插入从表
|
|
|
+ if (insertDetails.size()>0) {
|
|
|
+ prodIODetailMapper.batchInsert(insertDetails);
|
|
|
+ }
|
|
|
+ //更新从表
|
|
|
+ if (updateDetails.size()>0) {
|
|
|
+ prodIODetailMapper.batchUpdate(updateDetails);
|
|
|
+ }
|
|
|
+ saveDTO.setId(pi_id);
|
|
|
+ //更新已转数
|
|
|
+ if (!isbfaudit)
|
|
|
+ updateYqty(prodInOut);
|
|
|
+ return saveDTO;
|
|
|
+
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void delete(Long id) {
|
|
|
+ singleDelete(id);
|
|
|
+ }
|
|
|
+ private void singleDelete(Long id) {
|
|
|
+ if (null != id) {
|
|
|
+ //从表删除
|
|
|
+ prodIODetailMapper.deleteByForeignKey(id);
|
|
|
+ //主表删除
|
|
|
+ prodInOutMapper.deleteByPrimaryKey(id);
|
|
|
+ ProdInOut prodInOut = getMapper().selectByPrimaryKey(id);
|
|
|
+ //更新已转数
|
|
|
+ updateYqty(prodInOut);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public DocSavedDTO audit(ProdInOutFormDTO formData) {
|
|
|
+ Long id = null;
|
|
|
+ DocSavedDTO savedDTO = new DocSavedDTO();
|
|
|
+ if (null != formData) {
|
|
|
+ id = formData.getMain().getId();
|
|
|
+ if (StringUtils.isEmpty(id)) {
|
|
|
+ DocSavedDTO saveDTO = saveFormData(formData,true);
|
|
|
+ id = saveDTO.getId();
|
|
|
+ }
|
|
|
+ singleAudit(formData.getMain());
|
|
|
+ }
|
|
|
+ savedDTO.setId(id);
|
|
|
+ return savedDTO;
|
|
|
+ }
|
|
|
+ private void singleAudit(ProdInOutDTO prodInOutDTO) {
|
|
|
+ Map<String, Object> map = new HashMap<String, Object>();
|
|
|
+ map.put("inoutNo",prodInOutDTO.getPi_inoutno() );
|
|
|
+ map.put("class",prodInOutDTO.getPi_class() );
|
|
|
+ map.put("commitid",prodInOutDTO.getPi_recordmanid());
|
|
|
+ map.put("companyid",prodInOutDTO.getCompanyId());
|
|
|
+ map.put("result","");
|
|
|
+ //warehouseApi.post(map);
|
|
|
+ Object result = map.get("result");
|
|
|
+ System.out.println("result");
|
|
|
+ //记录日志
|
|
|
+ if (!StringUtils.isEmpty(result))
|
|
|
+ throw new BizException(500, "存在已审核单据,单据编号:" + result);
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void unAudit(Long id) {
|
|
|
+ ProdInOut prodInOut = getMapper().selectByPrimaryKey(id);
|
|
|
+ ProdInOutDTO prodInOutDTO = BeanMapper.map(prodInOut, ProdInOutDTO.class);
|
|
|
+ singleAudit(prodInOutDTO);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public void batchAudit(BatchDealBaseDTO baseDTOs) {
|
|
|
+ for (DocBaseDTO base : baseDTOs.getBaseDTOs()) {
|
|
|
+ Long id = base.getId();
|
|
|
+ ProdInOut prodInOut = getMapper().selectByPrimaryKey(id);
|
|
|
+ ProdInOutDTO prodInOutDTO = BeanMapper.map(prodInOut,ProdInOutDTO.class);
|
|
|
+ singleAudit(prodInOutDTO);
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public Integer deleteItem(Long id) {
|
|
|
+ Integer num = prodIODetailMapper.deleteByPrimaryKey(id);
|
|
|
+ ProdIODetail prodIODetail = prodIODetailMapper.selectByPrimaryKey(id);
|
|
|
+ ProdInOut prodInOut = getMapper().selectByPrimaryKey(prodIODetail.getPd_piid());
|
|
|
+ //更新已转数
|
|
|
+ updateYqty(prodInOut);
|
|
|
+ return num;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public void batchDelete(BatchDealBaseDTO baseDTOs) {
|
|
|
+ if (null == baseDTOs || null == baseDTOs.getBaseDTOs() ||
|
|
|
+ baseDTOs.getBaseDTOs().size() == 0)
|
|
|
+ return;
|
|
|
+ for (DocBaseDTO base : baseDTOs.getBaseDTOs()) {
|
|
|
+ singleDelete(base.getId());
|
|
|
+ }
|
|
|
+// Integer num = saledetailMapper.deleteByPrimaryKey(id);
|
|
|
+// DocBaseDTO docBaseDTO = getBaseDTOById(id);
|
|
|
+ }
|
|
|
+
|
|
|
+ @Override
|
|
|
+ public PageInfo<ProdInOutList> getListData(PageRequest page, ProdInOutReqDTO req) {
|
|
|
+ //设置默认分页
|
|
|
+ if (null == page || page.getSize() == 0 || page.getNumber() == 0) {
|
|
|
+ page = new PageRequest();
|
|
|
+ page.setNumber(1);
|
|
|
+ page.setSize(10);
|
|
|
+ }
|
|
|
+ PageHelper.startPage(page.getNumber(), page.getSize());
|
|
|
+ //查询数据
|
|
|
+ List<ProdInOutList> lists = getListByMode(req);
|
|
|
+ //取分页信息
|
|
|
+ PageInfo<ProdInOutList> pageInfo = new PageInfo<ProdInOutList>(lists);
|
|
|
+ return pageInfo;
|
|
|
+ }
|
|
|
+ private List<ProdInOutList> getListByMode(ProdInOutReqDTO req) {
|
|
|
+ List<ProdInOutList> list = null;
|
|
|
+ if (null == req || StringUtils.isEmpty(req.getMode()) || "Main".equals(req.getMode())) {
|
|
|
+ list = prodInOutListMapper.selectProdInOutListByCondition(req);
|
|
|
+ } else {
|
|
|
+ list = prodInOutListMapper.selectProdInOutBycondition(req);
|
|
|
+ }
|
|
|
+ return list;
|
|
|
+ }
|
|
|
+ @Override
|
|
|
+ public Result turnProdin(Long id) {
|
|
|
+ ProdInOut sourcePi = getMapper().selectByPrimaryKey(id);
|
|
|
+ Integer count=0;
|
|
|
+ double pdOutqty=0, pdYqty=0;
|
|
|
+ List<ProdIODetail> sourcePids =prodIODetailMapper.selectByFK(id);
|
|
|
+ //检查从表
|
|
|
+ for (ProdIODetail prodIODetail : sourcePids) {
|
|
|
+ pdOutqty = prodIODetail.getPd_outqty();
|
|
|
+ pdYqty = prodIODetail.getPd_yqty();
|
|
|
+ if (pdOutqty-pdYqty>0){
|
|
|
+ count++;
|
|
|
+ }
|
|
|
+ }
|
|
|
+ //判断可转数
|
|
|
+ if (count==0) {
|
|
|
+ return Result.error(ExceptionCode.TURNINNUM_NOT_EXIST);
|
|
|
+ }
|
|
|
+ //插入销售退货单主表
|
|
|
+ ProdInOut targetPi = new ProdInOut();
|
|
|
+ //生成单号
|
|
|
+ String piInoutno = "ProdInOut!SaleReturn"+Math.abs(Math.random()*10);
|
|
|
+ //maxnumberService.getMaxnumber("ProdInOut!Sale",true).getData();
|
|
|
+ targetPi.setPi_inoutno(piInoutno);
|
|
|
+ targetPi.setPi_class("销售退货单");
|
|
|
+ targetPi.setPi_date(new Date());
|
|
|
+ targetPi.setPi_recorddate(new Date());
|
|
|
+ //客户信息
|
|
|
+ targetPi.setPi_custid(sourcePi.getPi_custid());
|
|
|
+ targetPi.setPi_custcode(sourcePi.getPi_custcode());
|
|
|
+ targetPi.setPi_custname(sourcePi.getPi_custname());
|
|
|
+
|
|
|
+ targetPi.setPi_sacode(sourcePi.getPi_sacode());
|
|
|
+ targetPi.setPi_said(sourcePi.getPi_said());
|
|
|
+ //设置公司id
|
|
|
+ targetPi.setCompanyId(sourcePi.getCompanyId());
|
|
|
+ //保存数据
|
|
|
+ getMapper().insertSelective(targetPi);
|
|
|
+ //插入销售退货单从表
|
|
|
+ long pi_id = targetPi.getId();
|
|
|
+ for (int i = 0;i<sourcePids.size();i++){
|
|
|
+ ProdIODetail sourcePid = sourcePids.get(i);
|
|
|
+ ProdIODetail targetPid = new ProdIODetail();
|
|
|
+ if(sourcePid.getPd_inqty()-sourcePid.getPd_yqty()>0){
|
|
|
+ targetPid.setPd_piid(pi_id);
|
|
|
+ targetPid.setPd_inoutno(piInoutno);
|
|
|
+ targetPid.setPd_piclass("销售退货单");
|
|
|
+ targetPid.setPd_pdno(i);
|
|
|
+ targetPid.setPd_orderid(sourcePid.getPd_orderid());
|
|
|
+ targetPid.setPd_ordercode(sourcePid.getPd_ordercode());
|
|
|
+ targetPid.setPd_orderdetno(sourcePid.getPd_orderdetno());
|
|
|
+ targetPid.setPd_sendprice(sourcePid.getPd_sendprice());
|
|
|
+ targetPid.setPd_sdid(sourcePid.getPd_sdid());
|
|
|
+ targetPid.setPd_prodid(sourcePid.getPd_prodid());
|
|
|
+ targetPid.setPd_prodcode(sourcePid.getPd_prodcode());
|
|
|
+ targetPid.setPd_ioid(sourcePid.getId());
|
|
|
+ //公司id
|
|
|
+ targetPid.setCompanyId(sourcePid.getCompanyId());
|
|
|
+ //本次转单数
|
|
|
+ targetPid.setPd_inqty(sourcePid.getPd_outqty()-sourcePid.getPd_yqty());
|
|
|
+ prodIODetailMapper.insertSelective(targetPid);
|
|
|
+ //更新已转数
|
|
|
+ sourcePid.setPd_yqty(sourcePid.getPd_outqty());
|
|
|
+ prodIODetailMapper.updateByPrimaryKeySelective(sourcePid);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ return Result.success();
|
|
|
+
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Description: 检验获取并更新单号
|
|
|
+ * @Param: [code, id]
|
|
|
+ * @return: java.lang.String
|
|
|
+ * @Author: guq
|
|
|
+ * @Date: 2018/10/19
|
|
|
+ */
|
|
|
+ private String pushMaxnubmer(String code, Long id,String pi_class) {
|
|
|
+// if (null == code) {
|
|
|
+// throw new BizException(BizExceptionCode.NULL_CODE);
|
|
|
+// }
|
|
|
+ Integer count = "0".equals(String.valueOf(id)) ? saleMapper.validateCodeWhenInsert(code) :
|
|
|
+ saleMapper.validateCodeWhenUpdate(code, id);
|
|
|
+ String caller = "";
|
|
|
+ if(pi_class.equals("销售出货单")){
|
|
|
+ caller = "ProdInOut!Sale";
|
|
|
+ }else {//销售退货单
|
|
|
+ caller = "ProdInOut!SaleReturn";
|
|
|
+ }
|
|
|
+ if (null == code) {
|
|
|
+ return caller+Math.random()*10;
|
|
|
+ }else{
|
|
|
+ return code;
|
|
|
+ }
|
|
|
+ //return maxnumberService.pushMaxnubmer(count, code, caller).getData();
|
|
|
+ }
|
|
|
+ /**
|
|
|
+ * @Description: 更新已转数
|
|
|
+ * @Param: [code, id]
|
|
|
+ * @return: java.lang.String
|
|
|
+ * @Author: wuyx
|
|
|
+ * @Date: 2018/10/23
|
|
|
+ */
|
|
|
+ private void updateYqty(ProdInOut prodInOut) {
|
|
|
+ //更新已转数
|
|
|
+ if ("销售出货单".equals(prodInOut.getPi_class())){
|
|
|
+ //saledetailMapper.updateSaleOutYqty(prodInOut.getPi_puid());
|
|
|
+ }else if ("销售退货单".equals(prodInOut.getPi_class())){
|
|
|
+ prodIODetailMapper.updateSaleOutYqty(prodInOut.getPi_said());
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|