package com.xxfc.platform.vehicle.biz;


import com.github.wxiaoqi.security.common.biz.BaseBiz;
import com.github.wxiaoqi.security.common.msg.ObjectRestResponse;
import com.github.wxiaoqi.security.common.util.process.ResultCode;
import com.github.wxiaoqi.security.common.vo.PageDataVO;
import com.xxfc.platform.vehicle.entity.*;
import com.xxfc.platform.vehicle.mapper.BranchCompanyStockInfoMapper;
import com.xxfc.platform.vehicle.mapper.CompanyBaseMapper;
import com.xxfc.platform.vehicle.pojo.dto.CompanyBaseDetailDTO;
import com.xxfc.platform.vehicle.pojo.vo.CompanyBaseVo;
import com.xxfc.platform.vehicle.pojo.vo.CompanyVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.util.List;

@Service
@Slf4j
public class CompanyBaseBiz extends BaseBiz<CompanyBaseMapper, CompanyBase>  {


     @Autowired
    BranchCompanyStockRightBiz stockRightBiz;

    @Autowired
    BranchCompanyBiz branchCompanyBiz;

     @Autowired
     BranchCompanyStockInfoMapper stockInfoMapper;

    @Value("${branchCompanyPic.url}")
    private  String companyUrl;

    //临时数据同步
    public ObjectRestResponse synchro(){
       List<BranchCompanyStockInfo> list= stockInfoMapper.selectAll();
       if (list.size()>0){
           for (BranchCompanyStockInfo companyStockInfo:list){
                int num=0;
               String companyName=companyStockInfo.getCompanyName();
               Integer balance=companyStockInfo.getBalance();
               Integer total=companyStockInfo.getTotal();
               BigDecimal price=companyStockInfo.getPrice();
               CompanyBase companyBase=new CompanyBase();
               companyBase.setAddrCity(companyStockInfo.getAddrCity());
               companyBase.setAddrProvince(companyStockInfo.getAddrProvince());
               companyBase.setName(companyName);
               companyBase.setCover(companyUrl+"/image/admin/upload"+companyStockInfo.getCompanyPic());
               companyBase.setImages(companyUrl+"/image/admin/upload"+companyStockInfo.getCompanyPic());
               insertSelective(companyBase);
               Integer companyBaseId=companyBase.getId();
               if (companyBaseId!=null){
                    BranchCompanyStockRight branchCompanyStockRight=new BranchCompanyStockRight();
                   branchCompanyStockRight.setBalance(balance);
                   branchCompanyStockRight.setPrice(price);
                   branchCompanyStockRight.setTotal(total);
                   branchCompanyStockRight.setCompanyBaseId(companyBaseId);
                   branchCompanyStockRight.setStockState(2);
                   stockRightBiz.insertSelective(branchCompanyStockRight);
               }
               num++;
               log.info("----成功---num=="+num+"---companyBaseId==="+companyBaseId);
           }
       }

        return  ObjectRestResponse.succ();
    }
    //设置基础信息
    public  ObjectRestResponse updCompany(CompanyVo companyVo){
        if (companyVo==null|| StringUtils.isBlank(companyVo.getCompanyName())|| StringUtils.isBlank(companyVo.getName())||
                companyVo.getZoneId()==null||companyVo.getZoneId()==0|| companyVo.getAddrProvince()==null||companyVo.getAddrProvince()==0
                || companyVo.getAddrCity()==null||companyVo.getAddrCity()==0){
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        Integer state=companyVo.getState()==null?2:companyVo.getState();
        if (state==1&&(StringUtils.isBlank(companyVo.getAddrDetail())||StringUtils.isBlank(companyVo.getImages())||StringUtils.isBlank(companyVo.getDescribes())||StringUtils.isBlank(companyVo.getContent())
                ||StringUtils.isBlank(companyVo.getLeader())||StringUtils.isBlank(companyVo.getLeaderContactInfo())||StringUtils.isBlank(companyVo.getTourServicePhone())||StringUtils.isBlank(companyVo.getVehiceServicePhone()))){
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"上架时参数不能为空");
        }
        CompanyBase companyBase=new CompanyBase();
        BeanUtils.copyProperties(companyVo,companyBase);
        Integer companyBaseId=companyVo.getId();
        if (companyBaseId==null||companyBaseId==0){
            insertSelective(companyBase);
            companyBaseId=companyBase.getId();
        }else {
            updateSelectiveById(companyBase);
        }
        BranchCompany branchCompany=new BranchCompany();
        BeanUtils.copyProperties(companyVo,branchCompany);
        branchCompany.setCompanyBaseId(companyBaseId);
        branchCompany.setName(companyVo.getCompanyName());
        Integer companyId=companyVo.getCompanyId();
        if (companyId==null||companyId==0){
            branchCompany.setId(null);
            branchCompanyBiz.insertSelective(branchCompany);
        }else {
            branchCompany.setId(companyId);
            branchCompanyBiz.updateSelectiveById(branchCompany);
        }
        return  ObjectRestResponse.succ();
    }

    //获取详情
    public  ObjectRestResponse getCompanyDetail(CompanyVo companyVo){
        if (companyVo==null){
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        Integer companyBaseId=companyVo.getId();
        Integer companyId=companyVo.getCompanyId();
        Integer stockId=companyVo.getStockId();
        if (companyBaseId!=null&&companyBaseId>0){
            CompanyBase companyBase=selectById(companyBaseId);
            BeanUtils.copyProperties(companyBase,companyVo);
        }
        if (companyId!=null&&companyId>0){
            BranchCompany branchCompany=branchCompanyBiz.selectById(companyId);
            BeanUtils.copyProperties(branchCompany,companyVo);
        }
        if (stockId!=null&&stockId>0){
            BranchCompanyStockRight stockRight=stockRightBiz.selectById(stockId);
            BeanUtils.copyProperties(stockRight,companyVo);
        }
        return  ObjectRestResponse.succ(companyVo);
    }

    //设置股权信息
    public  ObjectRestResponse updStockInfo(CompanyVo companyVo){

        if (companyVo==null|| companyVo.getId()==null||companyVo.getId()==0 || companyVo.getBalance()==null||companyVo.getBalance()==0|| companyVo.getTotal()==null||companyVo.getTotal()==0
                ||companyVo.getPrice() ==null|| companyVo.getPrice().compareTo(BigDecimal.ZERO)<0){
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        Integer companyBaseId=companyVo.getId();
        Integer stockState=companyVo.getStockState();
        if (stockState!=null&&stockState==2){
            CompanyBase companyBase=selectById(companyBaseId);
            if (companyBase==null||StringUtils.isBlank(companyBase.getDescribes())|| StringUtils.isBlank(companyBase.getContent())||StringUtils.isBlank(companyBase.getImages())){
                return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"基础信息不完善");
            }
        }
        Integer stockId=companyVo.getStockId();
        BranchCompanyStockRight right=new BranchCompanyStockRight();
        BeanUtils.copyProperties(companyVo,right);
        if (stockId==null||stockId==0){
            right.setId(null);
            stockRightBiz.insertSelective(right);
        }else {
            right.setId(stockId);
            stockRightBiz.updateSelectiveById(right);
        }
        return  ObjectRestResponse.succ();
    }

    //设置基础信息
    public  ObjectRestResponse updCompanyDetail(CompanyVo companyVo){
        if (companyVo==null){
            return  ObjectRestResponse.createFailedResult(ResultCode.NULL_CODE,"参数不能为空");
        }
        CompanyBase companyBase=new CompanyBase();
        BeanUtils.copyProperties(companyVo,companyBase);
        Integer companyBaseId=companyVo.getId();
        if (companyBaseId==null||companyBaseId==0){
            insertSelective(companyBase);
            companyBaseId=companyBase.getId();
        }else {
            updateSelectiveById(companyBase);
        }
        BranchCompany branchCompany=new BranchCompany();
        BeanUtils.copyProperties(companyVo,branchCompany);
        branchCompany.setCompanyBaseId(companyBaseId);
        Integer companyId=companyVo.getCompanyId();
        if (companyId==null||companyId==0){
            branchCompany.setId(null);
            branchCompanyBiz.insertSelective(branchCompany);
        }else {
            branchCompany.setId(companyId);
            branchCompanyBiz.updateSelectiveById(branchCompany);
        }
        return  ObjectRestResponse.succ();
    }

    public  ObjectRestResponse delCompany(Integer id){
        CompanyBase companyBase=new CompanyBase();
        companyBase.setId(id);
        companyBase.setIsDel(1);
        updateSelectiveById(companyBase);
        return  ObjectRestResponse.succ();
    }

    public  ObjectRestResponse getList(CompanyVo companyVo){
        Integer page=1;
        Integer limit=10;
        if (companyVo.getPage()!=null&&companyVo.getPage()>0){
            page=companyVo.getPage();
        }
        if (companyVo.getLimit()!=null&&companyVo.getLimit()>0){
            limit=companyVo.getLimit();
        }
        return  ObjectRestResponse.succ(PageDataVO.pageInfo(page, limit, ()->mapper.getList(companyVo)));
    }

    

    public CompanyBaseVo findCompanyDetailById(Integer id) {
        CompanyBaseVo companyBaseVo = new CompanyBaseVo();
        CompanyBaseDetailDTO companyBaseDetailDTO = mapper.findCompanyBaseById(id);
        BeanUtils.copyProperties(companyBaseDetailDTO,companyBaseVo);
        return companyBaseVo;
    }

}
